gpt4 book ai didi

node.js - NodeJS 中的 Gmail 身份验证

转载 作者:太空宇宙 更新时间:2023-11-04 00:16:10 24 4
gpt4 key购买 nike

我想使用访问 token 进行 Gmail 身份验证。我正在传递像 ,

这样的值
const options = {
method: 'GET',
uri: 'https://www.googleapis.com/auth/plus.me',
qs: {
access_token: req.body.gmailToken,
fields: 'id,name,first_name,last_name,email,picture'
}
};

但我无法从 gmail 获取身份验证数据。

还有其他方法可以通过传递 accessToken 来仅从 Gmail 获取身份验证数据吗?

最佳答案

uri: 'https://www.googleapis.com/auth/plus.me',

是 Google+ api 的端点,而不是 gmail api 的端点。 Gmail 端点如下所示

https://www.googleapis.com/gmail/v1/

以下教程邮件帮助Node.js Quickstart - Gmail

Gmail 身份验证

/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
*
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
}

关于node.js - NodeJS 中的 Gmail 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46924703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com