【问题标题】:Google OAuth 2.0 Access Token Request ErrorGoogle OAuth 2.0 访问令牌请求错误
【发布时间】:2016-11-03 19:50:21
【问题描述】:

我在几个月前“完成”的 GitHub 上有一个小型 Google App Engine 项目。

它一直工作到现在(最后一次使用是 2-3 个月前),但 Google 似乎改变了一些东西,我不知道真正的问题是什么以及如何解决它。

问题出在这个文件中(第 59 行): File

代码:

public class TokenManager {

static private final String TOKEN_URL = "https://www.googleapis.com/oauth2/v3/token";
static private final String ENC = "UTF-8";

private String _refreshToken;
private String _clientId;
private String _clientSecret;

private final Gson _gson = new Gson();
private TokenData _tokenData = new TokenData();

...

private void refreshAccessToken() throws Exception {
    String payload = "client_secret=$client_secret&grant_type=refresh_token&refresh_token=$refresh_token&client_id=$client_id";
    payload = payload.replace("$client_secret", URLEncoder.encode(_clientSecret, ENC));
    payload = payload.replace("$refresh_token", URLEncoder.encode(_refreshToken, ENC));
    payload = payload.replace("$client_id", URLEncoder.encode(_clientId, ENC));

    HTTPRequest request = new HTTPRequest(new URL(TOKEN_URL), HTTPMethod.POST);
    request.setPayload(payload.getBytes(ENC));

    String stringData = new String(URLFetchServiceFactory.getURLFetchService().fetch(request).getContent(), ENC);

    _tokenData = _gson.fromJson(stringData, TokenData.class);
}

}

问题在于访问令牌请求。 (我使用离线刷新令牌。)

回复是这样的:

{
    "error": "invalid_grant",
    "error_description": "Bad Request",
    "error_uri": ""
}
  1. 我已尝试将 TOKEN_URL 修改为“https://www.googleapis.com/oauth2/v4/token
  2. 我已尝试生成新的刷新令牌。
  3. 我已尝试创建新的 OAuth 客户端 ID。

所有尝试都有同样的问题。

有人知道出了什么问题吗?

感谢您的帮助!

【问题讨论】:

  • 请发布您的代码。
  • 准备好了。抱歉,在我看来,没有代码 sn-p 的帖子更透明,但我修复了它。

标签: java google-app-engine oauth-2.0 google-api google-oauth


【解决方案1】:

我发现了问题。

可能我之前的刷新令牌已过期。我试图生成 一个新的,但我误解了这个过程,我考虑过“代码” 参数作为刷新令牌。

https://developers.google.com/identity/protocols/OAuth2WebServer#offline

【讨论】:

    猜你喜欢
    • 2012-11-06
    • 2012-09-29
    • 2019-09-16
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2017-06-16
    • 2011-09-19
    • 1970-01-01
    相关资源
    最近更新 更多