der*_*fed 9 google-api-nodejs-client google-auth-library
我正在使用googleapisnpm 库授权使用 OAuth2 访问我的 Gmail 帐户以发送电子邮件。我正在使用以下代码(TypeScript)来做到这一点:
const oAuth2Client = new google.auth.OAuth2(
googleConfig.clientId,
googleConfig.clientSecret
);
oAuth2Client.setCredentials({
refresh_token: googleConfig.refreshToken
});
const accessTokenRetVal = (await oAuth2Client.refreshAccessToken()).credentials.access_token;
const accessToken = accessTokenRetVal || '';
Run Code Online (Sandbox Code Playgroud)
此代码有效,但我收到以下消息:
(node:8676) [google-auth-library:DEP007] DeprecationWarning: The `refreshAccessToken` method has been deprecated, and will be removed in the 3.0 release of google-auth-library. Please use the `getRequestHeaders` method instead.
Run Code Online (Sandbox Code Playgroud)
我在 Google、GitHubgoogleapis和 StackOverflow上搜索过该模块,但找不到任何有关该getRequestHeaders方法构成的文档。我试过调用getRequestHeaders,但它似乎没有返回credentials带有访问令牌的对象。
是否有关于getRequestHeaders在这种情况下应该如何使用的官方文档?
小智 5
这个新函数直接为您提供一个“Headers”对象:
{ Authorization: 'Bearer xxxxxxxxx' }
Run Code Online (Sandbox Code Playgroud)
所以你可以直接使用它作为你的标头对象:
const authHeaders = await this.auth.getRequestHeaders();
yourfetchlibrary.get('https://......', {
headers: authHeaders,
})
Run Code Online (Sandbox Code Playgroud)
或者提取授权部分:
const authHeaders = await this.auth.getRequestHeaders();
yourfetchlibrary.get('https://......', {
headers: {
'Authorization': authHeaders.Authorization,
'Content-Type': 'application/json',
},
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2861 次 |
| 最近记录: |