首页 > 解决方案 > 如何从grantOfflineAccess返回的response_code中获取refresh_token

问题描述

我对 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”

标签: angulargoogle-calendar-apigoogle-api-js-client

解决方案


您从响应中获得的代码是您的服务器应该交换的一次性代码Access TokenRefresh Token. 请参考这个

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

希望能帮助到你。


推荐阅读