使用npm软件包googleapis,如何在验证用户的电子邮件地址后获得他们的电子邮件地址? [英] With the npm package googleapis how do I get the user's email address after authenticating them?
问题描述
我正在使用此npm库- https://www.npmjs.com/package/googleapis ,并且我将以下Express路由用作/user/
:
I'm using this npm library - https://www.npmjs.com/package/googleapis and I'm using the following Express routes as /user/
:
/* Redirect to the google login page */
router.get('/login', function (req, res) {
res.redirect(auth.generateUrl());
});
/* The callback from the google OAuth API call */
router.get('/callback', function (req, res) {
auth.authenticate(req.query.code);
res.send();
});
auth是此模块:
var oAuth2 = require('googleapis').auth.OAuth2;
var oauth2Client = new oAuth2([CLIENT_ID], [CLIENT_SECRET], [DOMAIN] + '/user/callback');
module.exports = {
/**
* Generate a url to redirect to for authenticating via Google
*
* @return {String}
*/
generateUrl: function () {
return oauth2Client.generateAuthUrl({
access_type: 'online', // 'online' (default) or 'offline' (gets refresh_token)
scope: ['https://www.googleapis.com/auth/userinfo.email'] // If you only need one scope you can pass it as string
});
},
authenticate: function (code) {
oauth2Client.getToken(code, function (err, tokens) {
console.log(err);
// Now tokens contains an access_token and an optional refresh_token. Save them.
if (!err) {
console.log(tokens);
oauth2Client.setCredentials(tokens);
}
});
}
};
以上身份验证功能基于 https://www中的示例. npmjs.com/package/googleapis#retrieve-access-token .
The authenticate function above is based on the example in https://www.npmjs.com/package/googleapis#retrieve-access-token.
现在,如果我转到/user/login
,我会看到google登录页面,然后要求我许可.我已经使用了上面的电子邮件范围,但是在返回的tokens
对象中看不到我的电子邮件地址.这就是我得到的:
Now if I go to /user/login
I see the google login page, which then asks me for permission. I have used the email scope above but I do not see my email address in the tokens
object that is returned. This is what I get:
{ access_token: '[72 length string]',
token_type: 'Bearer',
id_token: '[884 length string]',
expiry_date: [integer timestamp] }
这不是如何获取电子邮件地址吗?该文档不是很清楚,我在网上也没有找到示例教程,因为它们主要处理Google提供的特定服务,例如日历.我只对基本身份验证感兴趣.在文档中找不到其他任何可以获得范围信息的方法.
Is this not how to get the email address? The documentation is not very clear and neither are example tutorials I have found online, as they mainly deal with a specific service from google, like the calendars. I'm only interested in basic authentication. I can't find any other methods that might get scope information in the documentation.
还有一点要注意,但是当用户登录时,我是否必须在每个请求上都调用getToken()
?
A minor point as well, but do I have to call getToken()
on every request when a user is logged in?
在深入研究库的代码后,我发现了这一点:
After some digging around in the code for the library, I found this:
this.userinfo = {
/**
* oauth2.userinfo.get
*
* @desc Get user info
*
* @alias oauth2.userinfo.get
* @memberOf! oauth2(v1)
*
* @param {object=} params - Parameters for request
* @param {callback} callback - The callback that handles the response.
* @return {object} Request object
*/
get: function(params, callback) {
var parameters = {
options: {
url: 'https://www.googleapis.com/oauth2/v1/userinfo',
method: 'GET'
},
params: params,
requiredParams: [],
pathParams: [],
context: self
};
return createAPIRequest(parameters, callback);
}
这同时在node_modules/googleapis/apis/oauth2/v1.js
和node_modules/googleapis/apis/oauth2/v1.js
中.但是,这似乎不是require('googleapis').auth.OAuth2
所使用的,而是node_modules/google-auth-library/lib/auth/oauth2client.js
.有访问userinfo.get
的方法吗?
This is in both node_modules/googleapis/apis/oauth2/v1.js
and node_modules/googleapis/apis/oauth2/v1.js
. However, this doesn't appear to be what require('googleapis').auth.OAuth2
uses, which is node_modules/google-auth-library/lib/auth/oauth2client.js
. Is there a way of accessing userinfo.get
?
进一步编辑
我找到了本教程- https://www.theodo.fr/blog/2014/06/dont-bother-with-keys-open-your-door-with-google-api/,此部分哪一个(页面底部附近)正是我想要做的:
I found this tutorial - https://www.theodo.fr/blog/2014/06/dont-bother-with-keys-open-your-door-with-google-api/, this section of which (near the bottom of the page) is exactly what I want to do:
googleapis.discover('oauth2', 'v1').execute(function(err, client) {
if (!err) {
client.oauth2.userinfo.get().withAuthClient(oauth2Client).execute(function(err, results) {
var email = results.email;
if ((email.indexOf('theodo.fr') + 'theodo.fr'.length) != email.length) {
return res.send({
status: -1,
message: "Google Plus authentication failed (domain mismatch)"
});
}
doorClient.open();
res.send({
status: 0,
message: 'Door opened. Welcome !'
});
});
}
});
不考虑Google API的绝对荒谬,此代码不再起作用. discover
不再是一个函数,所以我不知道如何访问包含我需要的userinfo.get
函数的v1
或v2
.
Leaving aside the absolute ridiculous verbosity of Google's API, this code no longer works. discover
is no longer a function so I've no idea how to access v1
or v2
which contain the userinfo.get
function that I need.
推荐答案
对于我没有的版本2.1.6,使其有效的方法是:
With the version I have right not, which is 2.1.6, the way to make it work is:
googleapis.oauth2("v2").userinfo.v2.me.get({auth: oauth2Client}, (e, profile) => {
...
});
我必须研究源代码以找出方法,但我不确定100%是否是最好的方法,因为我不得不两次提及"v2".但这对我有用.
I have to looked into the source code to figure out how to do it and I am not 100% sure whether this is the best way because I have to mention "v2" twice. But it works for me.
这篇关于使用npm软件包googleapis,如何在验证用户的电子邮件地址后获得他们的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!