无法生成刷新访问令牌

Unable to generate refresh Access token

本文关键字:刷新 访问令牌      更新时间:2023-09-26

我能够生成访问令牌,但刷新令牌的响应对象返回 null 以下是我的代码。

JavaScript:

 function connect_dfa(oauthurl,scop,redirect,clientId) {

            var width = 1024;
            var height = 512;
            var left = (screen.width / 2) - (width / 2);
            var top = (screen.height / 2) - (height / 2);
            var specs = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,width='+width+',height='+height+',top='+top+',left='+left;
            var url = oauthurl+"&scope="+scop+"&redirect_uri="+redirect+"&response_type=code&client_id="+clientId;
            alert (url);
            var win = window.open(url, 'scgid platform', specs, false);
        return false;
    }
重定向

到重定向 URL 后,以下代码使用身份验证代码生成访问令牌:

flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, SCOPES).build();

final TokenResponse response = flow.newTokenRequest(dfaToken).setRedirectUri(CALLBACK_URI)
        .execute();
System.out.println(response.getAccessToken());
System.out.println(response.getRefreshToken());

它向我返回一个访问令牌,例如:ya29.CgH2ZBKtHUBr6uJtOs8q0q2vf_tllv_UYMF-Vcd-bODGOgoxqz05mzfDkymEGjVdmYuw2Os4FFpQPQ"但刷新令牌的重新变为空。

我做错了什么,因为我能够生成访问令牌。

您需要

"&access_type=offline"添加到授权窗口 url 才能获取刷新令牌。详情: https://developers.google.com/accounts/docs/OAuth2WebServer#formingtheurl

生成刷新令牌的步骤:

步骤 1 :将应用的返回网址配置为 http://0.0.0.0。这样它就会使用代码将您重定向到本地主机。例 : http://0.0.0.0/?state=authenticated&code=<code>

第 2 步:使用客户端代码导航到以下 url

https://account.box.com/api/oauth2/authorize?response_type=code&client_id=<client_id>&state=authenticated

第 3 步:授予对盒子 api 的访问权限,然后从您的 URL 复制<code>。例 : http://0.0.0.0/?state=authenticated&code=<code>

第 4 步:使用以下参数将请求发布到 https://api.box.com/oauth2/token

grant_type = authorization_code,
    client_id = <client_id>,
    client_secret = <client_secret>,
    code = code you generated in step 3

这将返回以下对象:

access_token,
expires_in, 
restricted_to, 
refresh_token, 
token_type, 
created_time

刷新令牌到期:60 天。

访问令牌到期:60分钟。

注意:在 10 秒内使用代码

注意:每次重新生成access_token令牌时,您都会获得新的刷新令牌。有关更多信息,请参阅框文档