如何在 Java 中使用刷新令牌获取访问令牌? [英] How to get an access token using a refresh token in Java?

查看:31
本文介绍了如何在 Java 中使用刷新令牌获取访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Java 中的 Google Contact API 实现 Contact Application.我已完成授权步骤并获得一个访问令牌和一个刷新令牌.

I am currently implementing Contact Application using Google Contact API in Java. I have completed steps of authorization and obtained an access token and a refresh token.

现在我有了 CLIENT_ID , CLIENT_SECRET AND REFRESH_TOKEN .但是 访问令牌 将在一小时内过期.

Now I have the CLIENT_ID , CLIENT_SECRET AND REFRESH_TOKEN with me . But the access token is getting expired within an hour.

谁能告诉我如何在 Java 中使用 刷新令牌 自动生成访问令牌?

Can someone tell how to automatically generate an access token using a refresh token in Java?

推荐答案

您可以使用 Google OAuth2 客户端库通过 刷新令牌 获取新的访问令牌.

You can use Google OAuth2 client library for getting a new access token using a refresh token.

这是我获取新的访问令牌的代码:

Here is my code for getting a new access token:

public TokenResponse refreshAccessToken(String refreshToken) throws IOException {
    TokenResponse response = new GoogleRefreshTokenRequest(
            new NetHttpTransport(),
            new JacksonFactory(),
            refreshToken, 
            "your clientId",
            "your clientSecret")
            .execute();
    System.out.println("Access token: " + response.getAccessToken());

    return response;
}

<小时>

有关更多信息,请阅读官方 Google API 指南:


For more information read the official Google API guide:

这篇关于如何在 Java 中使用刷新令牌获取访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆