【问题标题】:Nodejs Google Drive APINodejs 谷歌驱动 API
【发布时间】:2013-08-21 20:11:16
【问题描述】:

我正在尝试了解 nodejs Google Drive API 的授权(特别是刷新令牌)如何工作。

这是来自https://github.com/google/google-api-nodejs-client的代码。

    oauth2Client.credentials = {
      access_token: 'ACCESS TOKEN HERE',
      refresh_token: 'REFRESH TOKEN HERE'
    };

    client
      .plus.people.get({ userId: 'me' })
      .withAuthClient(oauth2Client)
      .execute(callback);

一般问题: 刷新令牌实际上如何与访问令牌一起使用?

背景: 正如我所解释的,每个访问令牌都有一个有限的时间跨度(~1 小时)。因此,当用户第一次连接到我的服务器(服务器提供用户身份验证机制)时,服务器会收到有限寿命的访问令牌和一次性刷新令牌。 1 小时后,访问令牌过期。

具体问题: 这里涉及到关键问题。到期后,我的服务器仍然使用 EXPIRED 访问令牌和刷新令牌向 Google Drive Api 发送请求(服务器使用会话来存储这些值)。这仍然可以访问 Google Drive 中的内容吗?我的猜测是nodejs lib + google drive api足够智能,可以检测到访问令牌已过期并识别刷新令牌&api用新的访问令牌盲目替换过期的访问令牌(就像服务器不需要做任何事情;只是谷歌驱动器API)。 api会用新的访问码响应服务器吗?

我需要弄清楚这一点,因为我需要在 Nodejs 中有效地组织我的代码。

谢谢!

【问题讨论】:

    标签: node.js google-docs-api


    【解决方案1】:

    是的。

    Node.js API 客户端会检测访问令牌错误并自动刷新令牌。

    你可以看到这个in the source

    var hasAuthError = res.statusCode == 401 || res.statusCode == 403;
    // if there is an auth error, refresh the token
    // and make the request again
    if (!opt_dontForceRefresh && hasAuthError && credentials.refresh_token) {
      // refresh access token and re-request
      that.refreshToken_(credentials.refresh_token, function(err, result) {
        if (err) {
          opt_callback && opt_callback(err, null, null);
        } else {
          var tokens = result;
          tokens.refresh_token = credentials.refresh_token;
          that.credentials = tokens;
          that.request(opts, opt_callback, true);
        }
      });
    } 
    

    【讨论】:

    • 那么,这是否意味着oauth2Client.credentials的access_token会自动更新???
    猜你喜欢
    • 1970-01-01
    • 2012-07-04
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多