【问题标题】:Getting Gmail API access and ID token, but refresh token is NULL获取 Gmail API 访问权限和 ID 令牌,但刷新令牌为 NULL
【发布时间】:2017-11-05 13:28:35
【问题描述】:

按照https://developers.google.com/identity/sign-in/web/server-side-flow从JavaScript获取授权码,并将其传递给服务器端,我们确实得到了一个访问令牌(和一个ID令牌),但不是所需的刷新令牌。

周围有很多帖子,但还没有解决。

关于如何获取刷新令牌有什么建议吗?

谢谢!

    private String getResponseToken(GoogleClientSecrets clientSecrets,
                                                 String authCode) throws IOException {
        try {
            GoogleTokenResponse tokenResponse =
                    new GoogleAuthorizationCodeTokenRequest(
                            new NetHttpTransport(),
                            JacksonFactory.getDefaultInstance(),
                            "https://www.googleapis.com/oauth2/v4/token",
//                        "https://accounts.google.com/o/oauth2/token",
                            clientSecrets.getDetails().getClientId(),
                            clientSecrets.getDetails().getClientSecret(),

                            authCode, //NOTE: was received from JavaScript client

                            "postmessage" //TODO: what's this? 
                            ).execute();
            String accessToken = tokenResponse.getAccessToken();
            String idToken = tokenResponse.getIdToken();

            //TODO: not getting a refresh token... why?!
            String refreshToken = tokenResponse.getRefreshToken();
            Boolean hasRefreshToken = new Boolean(!(refreshToken == null));
            LOGGER.warn("received refresh token: {}", hasRefreshToken);

            LOGGER.debug("accessToken: {}, refreshToken: {}, idToken: {}", accessToken, refreshToken, idToken);
            return accessToken;
        }catch (TokenResponseException tre){...}

【问题讨论】:

  • 看起来不像是重复的。实际上,寻找在这篇文章中完成的第一步:“我已经完成了授权步骤并获得了访问令牌和刷新令牌。”。这就是我们需要的:获取刷新令牌。其他人在该帖子的底部指出,他使用 $client->setAccessType("offline");) 在 PHP 中获得了它。问题是如何使用 Java API 获取初始刷新令牌?

标签: oauth-2.0 google-oauth gmail-api


【解决方案1】:

Gmail API 仅在您第一次请求用户权限时提供刷新令牌。 (至少这是发生在我身上的事情)。

转到:https://myaccount.google.com/permissions?pli=1,删除对您应用的授权并运行您的代码。您应该会收到刷新令牌。

【讨论】:

  • 嗨,Ricardo,谢谢,但它似乎没有解决。删除应用授权将使权限提示再次显示给用户,但结果 - 没有获得刷新令牌 - 保持不变。
【解决方案2】:

你应该添加

  1. AccessType = "离线"

你需要调用函数

new GoogleAuthorizationCodeRequestUrl(...).setAccessType("offline") 

或其他语法:

var authReq = new GoogleAuthorizationCodeRequestUrl(new Uri(GoogleAuthConsts.AuthorizationUrl)) { 
    RedirectUri = Callback, 
    ClientId = ClientId, 
    AccessType = "offline", 
    Scope = string.Join(" ", new[] { Scopes... }), 
    ApprovalPrompt = "force" 
};

在 Fiddler 中,您应该会看到以下请求:

https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/webmasters&redirect_uri=http://mywebsite.com/google/scapi/callback/&response_type=code&client_id=xxx&access_type=offline

另见here

更多关于 setAccessType 的细节可以在here找到

【讨论】:

  • 将访问类型设置为离线有效,但我也必须按照 Ricardo Gómez 所说的去做
【解决方案3】:

在找到如何在后端使用 Google API 之后(文档有些不完整..),通过调整参数在前端解决了该问题:

grantOfflineAccess({ - 提示:'select_account' + 提示:'同意'

HTH

【讨论】:

    猜你喜欢
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2015-07-19
    • 2017-06-09
    • 2016-10-13
    • 1970-01-01
    • 2014-07-15
    相关资源
    最近更新 更多