简体   繁体   English

如何刷新服务器端(JAVA)上的access_token?

[英]How to refresh access_token on server side(JAVA)?

I am using a spring boot as my backend application. 我正在使用Spring Boot作为后端应用程序。 I have stored our client's access_token , refresh_token , and access_id in my postgresql database. 我已经将客户端的access_tokenrefresh_tokenaccess_id存储在我的postgresql数据库中。

Here is my code trying to get the new access token if token expired. 这是我的代码,如果令牌已过期,则试图获取新的访问令牌。

public void refreshGoogleIdToken(GoogleAuthEntity googleAuthEntity) {
        LOGGER.debug("GoogleAuthService.refreshGoogleIdToken()");
        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(transport)
                .setJsonFactory(jsonFactory)
                .setClientSecrets(googleAuthClientId, googleAuthClientSecret)
                .build();
        credential.setAccessToken(googleAuthEntity.getAccessToken());
        credential.setRefreshToken(googleAuthEntity.getRefreshToken());
        try {
            if (credential.refreshToken()) {
                Long newExpireTime = credential.getExpirationTimeMilliseconds();
                String newAccessToken = credential.getAccessToken();
                String newRefreshToken = credential.getRefreshToken();
                LOGGER.debug("NewAccessToken: " + newAccessToken);
                LOGGER.debug("NewRefreshToken: " + newRefreshToken);
                LOGGER.debug("NewExpireTime: " + newExpireTime);
            }
        } catch (IOException e) {
            LOGGER.debug("GoogleAuthService.refreshGoogleIdToken() - IOException");
            e.printStackTrace();
        }
    }

Google return 400 error, and the description is: 400 Bad Request Google返回400错误,描述为:400错误的请求

{
  "error" : "invalid_grant",
  "error_description" : "Bad Request"
}

What mistake that I have make? 我犯了什么错误? Thanks 谢谢

I have been using OAuth2 with spring framework and have only encountered this error "Invalid grant" in case if refresh token is invalid, expired, revoked or does not match the redirection uri used in the authorization request, or is issued to another client 我一直在将OAuth2与spring框架一起使用,并且仅在刷新令牌无效,已过期,已吊销或与授权请求中使用的重定向uri不匹配,或发给另一个客户端的情况下才遇到此错误“无效授权”

For your situation I think you should delete the stored refresh token / rectify it and debug your code again. 根据您的情况,我认为您应该删除存储的刷新令牌/对其进行更正,然后再次调试代码。 This may be due to wrong token info stored in your PostgreSQL database while testing. 这可能是由于测试时存储在PostgreSQL数据库中的令牌信息错误所致。

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

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