@@ -57,12 +57,15 @@ type Token struct {
57
57
}
58
58
59
59
// tokenJSON is the struct representing the HTTP response from OAuth2
60
- // providers returning a token in JSON form.
60
+ // providers returning a token or error in JSON form.
61
61
type tokenJSON struct {
62
- AccessToken string `json:"access_token"`
63
- TokenType string `json:"token_type"`
64
- RefreshToken string `json:"refresh_token"`
65
- ExpiresIn expirationTime `json:"expires_in"` // at least PayPal returns string, while most return number
62
+ AccessToken string `json:"access_token"`
63
+ TokenType string `json:"token_type"`
64
+ RefreshToken string `json:"refresh_token"`
65
+ ExpiresIn expirationTime `json:"expires_in"` // at least PayPal returns string, while most return number
66
+ Error string `json:"error"`
67
+ ErrorDescription string `json:"error_description"`
68
+ ErrorURI string `json:"error_uri"`
66
69
}
67
70
68
71
func (e * tokenJSON ) expiry () (t time.Time ) {
@@ -253,6 +256,13 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
253
256
if err != nil {
254
257
return nil , err
255
258
}
259
+ if tokenError := vals .Get ("error" ); tokenError != "" {
260
+ return nil , & TokenError {
261
+ Err : tokenError ,
262
+ ErrorDescription : vals .Get ("error_description" ),
263
+ ErrorURI : vals .Get ("error_uri" ),
264
+ }
265
+ }
256
266
token = & Token {
257
267
AccessToken : vals .Get ("access_token" ),
258
268
TokenType : vals .Get ("token_type" ),
@@ -269,6 +279,13 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
269
279
if err = json .Unmarshal (body , & tj ); err != nil {
270
280
return nil , err
271
281
}
282
+ if tj .Error != "" {
283
+ return nil , & TokenError {
284
+ Err : tj .Error ,
285
+ ErrorDescription : tj .ErrorDescription ,
286
+ ErrorURI : tj .ErrorURI ,
287
+ }
288
+ }
272
289
token = & Token {
273
290
AccessToken : tj .AccessToken ,
274
291
TokenType : tj .TokenType ,
@@ -292,3 +309,13 @@ type RetrieveError struct {
292
309
func (r * RetrieveError ) Error () string {
293
310
return fmt .Sprintf ("oauth2: cannot fetch token: %v\n Response: %s" , r .Response .Status , r .Body )
294
311
}
312
+
313
+ type TokenError struct {
314
+ Err string
315
+ ErrorDescription string
316
+ ErrorURI string
317
+ }
318
+
319
+ func (t * TokenError ) Error () string {
320
+ return fmt .Sprintf ("oauth2: error in token fetch repsonse: %s\n error_description: %s\n error_uri: %s" , t .Err , t .ErrorDescription , t .ErrorURI )
321
+ }
0 commit comments