gooauthoauth-2.0godoc

How to properly read errors from golang oauth2


token, err := googleOauthConfig.Exchange(context.Background(), code)
if err != nil {
 fmt.Fprintf(w, "Err: %+v", err)
}

The output of the fprintf is:

Err: oauth2: cannot fetch token: 401 Unauthorized
Response: {"error":"code_already_used","error_description":"code_already_used"}

I want to check if "error" = "code_already_used". For the life of me, I can't sort out how.

How do I check/return/read "error" or "error_description" of err?

I've looked at the oauth2 code and it's a bit above me.

// retrieveToken takes a *Config and uses that to retrieve an *internal.Token.
// This token is then mapped from *internal.Token into an *oauth2.Token which is returned along
// with an error..
func retrieveToken(ctx context.Context, c *Config, v url.Values) (*Token, error) {
    tk, err := internal.RetrieveToken(ctx, c.ClientID, c.ClientSecret, c.Endpoint.TokenURL, v)
    if err != nil {
        if rErr, ok := err.(*internal.RetrieveError); ok {
            return nil, (*RetrieveError)(rErr)
        }
        return nil, err
    }
    return tokenFromInternal(tk), nil
}

How guess I'm trying to see the (*RetrieveError) part. Right?

THANK YOU!


Solution

  • The expression:

    (*RetrieveError)(rErr)
    

    converts therErr's type from *internal.RetrieveError to *RetrieveError. And since RetrieveError is declared in the oauth2 package, you can type assert the error you receive to *oauth2.RetrieveError to get the details. The details are contained in that type's Body field as a slice of bytes.

    Since a slice of bytes is not the best format to be inspected and in your case it seems like the bytes contain a json object you can make your life easier by predefining a type into which you can unmarshal those details.

    That is:

    type ErrorDetails struct {
        Error            string `json:"error"`
        ErrorDescription string `json:"error_description"`
    }
    
    token, err := googleOauthConfig.Exchange(context.Background(), code)
    if err != nil {
        fmt.Fprintf(w, "Err: %+v", err)
    
        if rErr, ok := err.(*oauth2.RetrieveError); ok {
            details := new(ErrorDetails)
            if err := json.Unmarshal(rErr.Body, details); err != nil {
                panic(err)
            }
    
            fmt.Println(details.Error, details.ErrorDescription)
        }        
    }