1

I am using "golang.org/x/oauth2" library to fetch my access token but I am not receiving any refresh token or Expiry even if I remove my app from my authorized apps and restart.

Do you know why and how can I get an Expiry and a refresh token?

func (service *OAuthService) RequestJWT(config dto.OAuthConfig, ctx *gin.Context) (*oauth2.Token, error) {
    if config.State != RandomState {
        return nil, errors.New("state not valid")
    }

    token, err := GithubOauthConfig.Exchange(ctx, config.Code)

    fmt.Println("service:RequestJWT:AccessToken:", token.AccessToken)
    fmt.Println("service:RequestJWT:RefreshToken:", token.RefreshToken)
    fmt.Println("service:RequestJWT:Expiry:", token.Expiry)

    if err != nil {
        return nil, err
    }

    if !token.Valid() {
        return nil, errors.New("token not valid")
    }

    return token, nil
}

With this code I do receive an access token but refresh token and expiry are empty.

Thanks!

Thytu
  • 73
  • 4

1 Answers1

0

In order to get the refresh_token you need to include access_type=offline in the OAuth request URL.

Please refer the official documentation of oauth2

# Configure authorization url
client.authorize_url(
  scope: "https://www.googleapis.com/auth/analytics.readonly profile",
  redirect_uri: callback_url,
  access_type: "offline",
  prompt: "select_account"
)

OR

You can refer this link for more information.

Ashutosh Singh
  • 721
  • 3
  • 6
  • Unfortunately, it's still doesn't work... ``` token, err := GithubOauthConfig.Exchange(ctx, config.Code, oauth2.AccessTypeOffline) ``` RefreshToken and Expiry are still empty. Any other idea? – Thytu Dec 02 '21 at 22:35