Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 如何从OAuth2 Google API获取电子邮件和个人资料信息?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用OAuth2 API使用Google Api Node.js客户端检索已登录用户名称.

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

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

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

With the npm package googleapis how do I get the user’s email address after authenticating them?

看看像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){
       ...
     }
);

但似乎OAuth2并不像那样……

解决方法

您可以将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'
    },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: '#####'
}

大佬总结

以上是大佬教程为你收集整理的node.js – 如何从OAuth2 Google API获取电子邮件和个人资料信息?全部内容,希望文章能够帮你解决node.js – 如何从OAuth2 Google API获取电子邮件和个人资料信息?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。