【问题标题】:Unable to generate refresh Access token无法生成刷新访问令牌
【发布时间】:2023-04-04 03:24:01
【问题描述】:

我能够生成访问令牌,但响应对象返回 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" 但为 Refresh Token 返回 NULL。

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

【问题讨论】:

标签: javascript oauth-2.0 google-oauth


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    生成刷新令牌的步骤:

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

    第 2 步:使用您的客户端代码导航到以下网址

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

    第 3 步: 授予对 box api 的访问权限,然后从您的 url 复制 &lt;code&gt;。 示例:http://0.0.0.0/?state=authenticated&amp;code=&lt;code&gt;

    第 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 令牌时,您都会获得新的刷新令牌。 For more information refer box documentations

    【讨论】:

      猜你喜欢
      • 2020-08-11
      • 2020-04-14
      • 2019-05-31
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 2017-08-11
      相关资源
      最近更新 更多