简体   繁体   English

(JAVA)Google API分析 - 无法使用“刷新令牌”获取新的“访问令牌”

[英](JAVA) Google API analytics - unable to obtain new “access token” using “refresh token”

I want to obtain a new "access token" based on the "refresh token" saved in database. 我想基于保存在数据库中的“刷新令牌”获取新的“访问令牌”。

Here is the code I wrote: 这是我写的代码:

GoogleCredential.Builder credentialBuilder = new GoogleCredential.Builder()
        .setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
        .setClientSecrets(CLIENT_ID, CLIENT_SECRET);
credentialBuilder.addRefreshListener(new MyCredentialRefreshListener());

credential = credentialBuilder.build();
credential.setRefreshToken("saved_refresh_token_from_database");

try {
    credential.refreshToken();
} catch (IOException e) {
    e.printStackTrace();
}


class MyCredentialRefreshListener implements CredentialRefreshListener {
    public void onTokenResponse(Credential cr, TokenResponse tr) {
       System.out.println("Credential was refreshed successfully.");
    }

    public void onTokenErrorResponse(Credential cr, TokenErrorResponse tr) {
        System.out.println(tr);
    }
 }

I get this message: 我收到这条消息:

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad com.google.api.client.auth.oauth2.TokenResponseException:400 Bad

Request { "error" : "invalid_grant" } 请求{“错误”:“invalid_grant”}

I use the same CLIENT_ID, CLIENT_SECRET and "refresh token" in a php script and there I managed to get the new "access token" using "refresh token" when "access token" expired. 我在php脚本中使用相同的CLIENT_ID,CLIENT_SECRET和“刷新令牌”,当“访问令牌”到期时,我设法使用“刷新令牌”获取新的“访问令牌”。

I wrote the code based on javadoc.google-oauth-java-client . 我根据javadoc.google-oauth-java-client编写了代码。

Any person here knows how to modify the code to obtain the new access token ? 这里的任何人都知道如何修改代码以获取新的访问令牌?

Thanks in advance. 提前致谢。

UPDATE: the problem was that I was saving in the database the refresh_token without doing a json_decode on it and it contained a "\\" which is considered escaped character in JSON. 更新:问题是我在数据库中保存了refresh_token而没有对它执行json_decode,它包含一个“\\”,在JSON中被视为转义字符。

It looks like you may have found some out of date documentation. 看起来您可能已经找到了一些过时的文档。 The JavaDoc you link is for version 1.8 of the client library. 您链接的JavaDoc适用于客户端库的1.8版本。 The current version is 1.12. 目前的版本是1.12。

The client library authors recommend that you use GoogleAuthorizationCodeFlow to manage your OAuth credentials. 客户端库作者建议您使用GoogleAuthorizationCodeFlow来管理OAuth凭据。 It takes care of refreshes automatically. 它会自动处理刷新。 If you follow this path, the code will look something like this: 如果您遵循此路径,代码将如下所示:

// Create the flow 
AuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow.Builder(
    new UrlFetchTransport(), new JacksonFactory(), CLIENT_ID, CLIENT_SECRET,
    Collections.singleton(OAUTH_SCOPES))
    .setAccessType("offline")
    .setCredentialStore(new AppEngineCredentialStore())
    .build();

// User Id: e.g. from session 
Credential credential = authorizationCodeFlow.loadCredential(USER_ID);     

// Make your API call. This example uses the Google+ API
// If the access token has expired, it will automatically refresh
Plus plus = new Plus(new UrlFetchTransport(), new JacksonFactory(), credential)
  .activities().list("me", "public").execute();

if you get http status 400, and message "invalid_grant". 如果您获得http状态400,并且消息“invalid_grant”。 I think you should check your instance of HTTP_TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET . 我认为你应该检查你的HTTP_TRANSPORT,JSON_FACTORY,CLIENT_ID,CLIENT_SECRET的实例

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

相关问题 Google Analytics:从刷新令牌获取访问令牌 - Google Analytics : Get access token from refresh token 具有服务帐户的Google Drive API中的访问令牌和刷新令牌为空 - Access Token and Refresh token are null in google drive api with service account 如何在 Java 中撤销 Google API 刷新令牌? - How to revoke Google API refresh token in Java? Android进程“静默”使用刷新令牌获取新的访问令牌 - Android process for “silently” using refresh token to get new access token 使用 java 创建刷新和访问令牌 jwt - Create refresh and access token jwt using java 尝试获取Google API的访问令牌时请求无效 - Invalid request when trying to obtain access token for Google API 如何在 Java 中使用刷新令牌获取访问令牌? - How to get an access token using a refresh token in Java? 无法刷新 paypal 的访问令牌 - Unable to refresh access token of paypal 如何获取离线令牌和刷新令牌以及自动刷新对 Google API 的访问 - How to get offline token and refresh token and auto-refresh access to Google API 无法获取eBay OAuth访问令牌 - Unable to Obtain eBay OAuth Access Token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM