如何从OAuth2 Google API获取电子邮件和个人资料信息?

aml*_*est 7 google-api node.js oauth-2.0

我正在尝试使用OAuth2 API使用Google API Node.js客户端检索已登录用户的名称.

按照用法示例,我设法进行登录,但我找不到获取配置文件信息的方法.

我没有使用People API也不使用Plus API,因为据我所知,OAuth2包含https://www.googleapis.com/auth/userinfo.profile,这应该足以完成任务.

我已经看到了一些类似的问题并尝试了这个问题的解决方案,但它没有用,也许它太旧了(?)

使用npm包googleapis如何在验证后获取用户的电子邮件地址?

看看像Google表格这样的其他API,可以像这样调用它们的函数:

var google = require('googleapis');
var sheets = google.sheets('v4');

...

sheets.spreadsheets.values.get({
    auth: auth,
    spreadsheetId: file_id,
    range: my_ranges,
  }, function(err, response){
       ...
     }
);
Run Code Online (Sandbox Code Playgroud)

但似乎OAuth2并不像那样......

Tan*_*ike 7

您可以将quickstart用于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())
});
Run Code Online (Sandbox Code Playgroud)
  • 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);
    }
});
Run Code Online (Sandbox Code Playgroud)

结果:

{
  id: '#####',
  name: '#####',
  given_name: '#####',
  family_name: '#####',
  link: '#####',
  picture: '#####',
  gender: '#####',
  locale: '#####'
}
Run Code Online (Sandbox Code Playgroud)


win*_*__7 5

2021年解决方案

这个答案可能会偏离最初提出的问题,但我认为这对于一些在后端通过生成 AuthUrl 并将其发送到客户端,然后在回调 URL 中接收数据响应来获取 google 用户信息的人来说会很有用。用户从客户端给予许可。

一些全局声明

import { google } from "googleapis";
const Oauth2Client = new google.auth.OAuth2(
  googleCredentials.CLIENT_ID,
  googleCredentials.CLIENT_SECRET,
  googleCredentials.REDIRECT_URI
);
Run Code Online (Sandbox Code Playgroud)

生成具有范围的 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
Run Code Online (Sandbox Code Playgroud)

在回调中获取用户数据

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
Run Code Online (Sandbox Code Playgroud)

如果有任何困惑或错误,请随时在评论中讨论