简体   繁体   English

Golang Oauth2 Github 没有获得刷新令牌

[英]Golang Oauth2 Github not getting refresh token

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.我正在使用“golang.org/x/oauth2”库来获取我的访问令牌,但即使我从授权的应用程序中删除我的应用程序并重新启动,我也没有收到任何刷新令牌或到期。

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!谢谢!

In order to get the refresh_token you need to include access_type=offline in the OAuth request URL.为了获得refresh_token ,您需要在 OAuth 请求 URL 中包含access_type=offline

Please refer the official documentation of oauth2请参考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.您可以参考此链接以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM