[英]How to get email and profile information from OAuth2 Google API?
我正在尝试使用 OAuth2 API 使用 Google API Node.js 客户端检索登录用户的名称。
按照用法示例,我设法进行了登录,但找不到获取个人资料信息的方法。
我没有使用 People API 或 Plus API,因为据我所知,OAuth2 包括https://www.googleapis.com/auth/userinfo.profile ,这应该足以完成任务。
我已经看到了一些类似的问题并尝试了这个的解决方案但是它没有用,也许它太旧了(?)
使用 npm 包 googleapis 如何在验证用户身份后获取用户的电子邮件地址?
查看其他 API,例如 Google 表格,可以这样调用它们的函数:
var google = require('googleapis');
var sheets = google.sheets('v4');
...
sheets.spreadsheets.values.get({
auth: auth,
spreadsheetId: file_id,
range: my_ranges,
}, function(err, response){
...
}
);
但似乎 OAuth2 不是那样工作的......
您可以使用 node.js 的快速入门。 详细信息是https://developers.google.com/gmail/api/quickstart/nodejs 。 使用 Quickstart 中的示例脚本,您可以通过 OAuth2 检索访问令牌,并检索电子邮件和用户配置文件。
在运行 Quickstart 示例之前,请确认先决条件、步骤 1 和步骤 2。
您可以通过如下更改listLabels(auth)
来使用。 范围是https://www.googleapis.com/auth/gmail.readonly
。
脚本:
var gmail = google.gmail({
auth: auth,
version: 'v1'
});
gmail.users.getProfile({
auth: auth,
userId: 'me'
}, function(err, res) {
if (err) {
console.log(err);
} else {
console.log(res);
}
});
gmail.users.messages.get({
'userId': 'me',
'id': 'mail ID',
'format': 'raw'
}, function (err, res) {
console.log(new Buffer(res.raw, 'base64').toString())
});
gmail.users.getProfile
检索用户配置文件。gmail.users.messages.get
检索电子邮件。如果我误解了你的问题,我很抱歉。
添加:
请将上面的脚本更改为以下脚本。 范围是https://www.googleapis.com/auth/userinfo.profile
。
脚本:
var oauth2 = google.oauth2({
auth: auth,
version: 'v2'
});
oauth2.userinfo.v2.me.get(
function(err, res) {
if (err) {
console.log(err);
} else {
console.log(res);
}
});
结果:
{
id: '#####',
name: '#####',
given_name: '#####',
family_name: '#####',
link: '#####',
picture: '#####',
gender: '#####',
locale: '#####'
}
2021年解决方案
这个答案可能会偏离最初提出的问题,但我认为这对于某些通过生成 AuthUrl 并将其发送到客户端然后在回调 URL 中接收数据响应而在后端获取 google 用户信息的人很有用用户从客户端授予权限。
一些全局声明
import { google } from "googleapis";
const Oauth2Client = new google.auth.OAuth2(
googleCredentials.CLIENT_ID,
googleCredentials.CLIENT_SECRET,
googleCredentials.REDIRECT_URI
);
使用范围生成 Auth URL
const SCOPE = [
'https://www.googleapis.com/auth/userinfo.profile', // get user info
'https://www.googleapis.com/auth/userinfo.email', // get user email ID and if its verified or not
];
const auth_url = Oauth2Client.generateAuthUrl({
access_type: "offline",
scope: SCOPE,
prompt: "consent",
state: "GOOGLE_LOGIN",
});
return res.json({ url: auth_url }); // send the Auth URL to the front end
在回调中获取用户数据
let code = req.query.code; // get the code from req, need to get access_token for the user
let { tokens } = await Oauth2Client.getToken(code); // get tokens
let oauth2Client = new google.auth.OAuth2(); // create new auth client
oauth2Client.setCredentials({access_token: tokens.access_token}); // use the new auth client with the access_token
let oauth2 = google.oauth2({
auth: oauth2Client,
version: 'v2'
});
let { data } = await oauth2.userinfo.get(); // get user info
console.log(data); // you will find name, email, picture etc. here
如果有任何混淆或错误,请随时在评论中讨论
您还可以查看 PassportJS。 他们有多种策略,包括 OAuth2 和 3 种不同的 Google Auth 策略。 我的回答并没有真正回答您的问题,但即使看一眼 Passport 的代码,您也可能会得到答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.