2013-08-21 181 views
0

我想了解如何为nodejs Google Drive API使用授权(特别是刷新令牌)。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云端硬盘中的内容吗?我猜测是nodejs lib + google驱动api足够SMART足以检测访问令牌已过期&刷新令牌被识别& api盲目地用新访问令牌代替过期访问令牌(如服务器不需要做任何事情;只是谷歌驱动器API)。 api会以新的访问码回应服务器吗?

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

谢谢!

回答

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); 
    } 
    }); 
} 
+0

那么,这是否意味着oauth2Client.credentials的会的access_token自动更新??? – user1657499