【问题标题】:How to get refresh_token from response_code return by grantOfflineAccess如何从grantOfflineAccess返回的response_code中获取refresh_token
【发布时间】:2021-06-21 02:43:26
【问题描述】:

我对 invalid_grant 问题感到震惊。我参考了 gapi doc 并像这样实现流程,

var authorisationRequestData =
  {
    'client_id': clientId,
    'scope': scopes,
    'immediate': immediate,
    prompt: 'consent',
    access_type: 'offline',
  include_granted_scope: true,
  }

  const authInstance = gapi.auth2.getAuthInstance();
  authInstance.grantOfflineAccess(authorisationRequestData)
    .then((res) => {
      console.log(gapi.auth.getToken());
      var all_token = JSON.stringify(gapi.auth.getToken());
      console.log("Token =" + all_token);
      console.log(res);
      console.log(res.code);

    }).catch(error => {
      console.log(error);
    });

我从上面的实现中获得了访问令牌和 response_code,并且能够针对用户创建日历事件。但是 1 小时后它给了我错误,例如“错误:invalid_grant,代码:400”。grantOfflineAccess 的令牌返回,例如“4/-QA8fj7FyvcPzlVwsapQwyqyKJs0MwkQlNdGhACVgOx3YSP5JamyEplViIx-uSV3JeAHrp9n0RZC0FMSX7IwAQk”

【问题讨论】:

    标签: angular google-calendar-api google-api-js-client


    【解决方案1】:

    您从响应中获得的代码是一次性代码,您的服务器应将其交换为Access TokenRefresh Token。请参考this

    在客户端应用程序上存储 refresh_token 不是最佳做法,因为它不安全。如果您只有客户端应用程序,我建议您关注link

    希望对您有所帮助。

    【讨论】:

    • 感谢您的链接,您有相同的样品吗?我不断收到“Bad request as error and error message is Invalid_grant”
    • 你能分享你的代码吗?您使用的是混合流吗?
    • 有问题的共享代码,您有相同的示例吗?
    • 如何与google交换一次性code获取access_token和refresh_token?
    • 我没有样品。但是,如果您可以创建存储库,我可以为此做出贡献。您需要使用您的凭据向 google 发送一次性代码以获取 access_token 和/或 refresh_token。请参考Libraries for Google APIs
    【解决方案2】:

    正如Nirav's answer 所说,您需要将这个一次性令牌换成一个可以随时使用的刷新令牌。

    您应该使用google-auth-library 在 Node.js 后端完成此工作流程。为此,您将使用身份验证代码来获取刷新令牌。但是,由于这是一个离线工作流程,您还需要验证所提供代码的完整性,如documentation explains

    const { OAuth2Client } = require('google-auth-library');
    
    /**
    * Create a new OAuth2Client, and go through the OAuth2 content
    * workflow. Return the refresh token.
    */
    function getRefreshToken(code, scope) {
      return new Promise((resolve, reject) => {
        // Create an oAuth client to authorize the API call. Secrets should be 
        // downloaded from the Google Developers Console.
        const oAuth2Client = new OAuth2Client(
          YOUR_CLIENT_ID,
          YOUR_CLIENT_SECRET,
          YOUR_REDIRECT_URL
        );
    
        // Generate the url that will be used for the consent dialog.
        const authorizeUrl = oAuth2Client.generateAuthUrl({
          access_type: 'offline',
          scope,
        });
        
        // Verify the integrity of the idToken through the authentication 
        // code and use the user information contained in the token
        const { tokens } = await client.getToken(code);
        const ticket = await client.verifyIdToken({
          idToken: tokens.id_token!,
          audience: keys.web.client_secret,
        });
        idInfo = ticket.getPayload();
        return tokens.refresh_token;
      })
    }
    

    使用此刷新令牌,您可以随时使用googleapis 库创建 Google API 客户端。我也在使用这个工作流来创建事件。对于带有 Node.js 后端的完整工作流程,您可能会发现 my gist 很有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 2017-07-25
      • 2017-11-09
      • 2019-06-02
      • 1970-01-01
      • 2015-05-26
      相关资源
      最近更新 更多