google mail API [[FIN]]
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m28s

This commit is contained in:
2023-12-04 13:48:11 +01:00
parent 8f13eb2f16
commit 358c238f3d
9 changed files with 549 additions and 131 deletions

View File

@@ -2,6 +2,8 @@ package googleapi
import (
"encoding/json"
"fmt"
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
"io"
@@ -11,6 +13,7 @@ import (
)
type GoogleOAuth interface {
AccessToken() (string, error)
}
type oauth struct {
@@ -41,16 +44,17 @@ func (c *oauth) AccessToken() (string, error) {
httpclient := http.Client{}
req, err := http.NewRequest(http.MethodPost, "https://oauth2.googleapis.com/token", nil)
url := fmt.Sprintf("https://oauth2.googleapis.com/token?client_id=%s&client_secret=%s&grant_type=%s&refresh_token=%s",
c.clientID,
c.clientSecret,
"refresh_token",
c.refreshToken)
req, err := http.NewRequest(http.MethodPost, url, nil)
if err != nil {
return "", err
}
req.URL.Query().Add("client_id", c.clientID)
req.URL.Query().Add("client_secret", c.clientSecret)
req.URL.Query().Add("grant_type", "refresh_token")
req.URL.Query().Add("refresh_token", c.refreshToken)
reqStartTime := time.Now()
res, err := httpclient.Do(req)
@@ -74,6 +78,10 @@ func (c *oauth) AccessToken() (string, error) {
return "", err
}
if r.ExpiresIn == 0 || r.AccessToken == "" {
return "", exerr.New(exerr.TypeGoogleResponse, "google oauth returned no response").Str("body", string(data)).Build()
}
c.lock.Lock()
c.expiryDate = langext.Ptr(reqStartTime.Add(timeext.FromSeconds(r.ExpiresIn - 10)))
c.accessToken = langext.Ptr(r.AccessToken)