谷歌的加弃用
我从谷歌说,使用了Google+的API被关闭电子邮件。我目前使用googleAPI.google.plus签署人在使用谷歌。难道这个插件将添加一个更新以支持授权用户使用谷歌的新途径?
环境细节:操作系统:Mac OS X版本的Node.js:v 10.8.0版本NPM:V6.5.0版本googleapis:33
const googleAPI = require('googleapis');
const plus = googleAPI.google.plus({
version: 'v1',
auth: configs.googleAPIKey // specify your API key here
});
// Check if Google tokens are valid
plus.people.get({
userId: googleUserId,
fields: 'displayName,emails,name,image',
access_token: googleAccessToken
})
.then((user) => {
logger.debug('From google: ' + util.inspect(user.data));
logger.debug('First Name: ' + user.data.name.givenName);
logger.debug('Last Name: ' + user.data.name.familyName);
})
回答如下:
你不显示您如何使用该对象做签到,所以这是一个有点难以回答。
然而,googleapis包已经支持登录插件与你的东西创建OAuth2 client像
const {google} = require('googleapis');
const oauth2Client = new google.auth.OAuth2(
YOUR_CLIENT_ID,
YOUR_CLIENT_SECRET,
YOUR_REDIRECT_URL
);
然后,您可以得到一个URL重定向他们,使他们可以登录喜欢的东西
const url = oauth2Client.generateAuthUrl({
// If you only need one scope you can pass it as a string
scope: scopes
});
然后重定向他们url
。一旦他们登录了网址,他们会被重定向到您指定作为YOUR_REDIRECT_URL
的URL,其中将包括一个名为code
参数。你需要这个代码,换取凭证,包括身份验证令牌
const {tokens} = await oauth2Client.getToken(code)
oauth2Client.setCredentials(tokens);
如果你只需要使用一个API Key(这就是你们的榜样暗示什么的),那么你应该只需要包括你现在的API做同样的方式调用,你需要做的关键。但是,这是不相关的授权。
因为它看起来像你想获得个人资料的信息,您可以使用类似用户信息或People API,然后选择你想为用户的字段。
使用用户信息可能看起来像
oauth2client.userinfo.get().then( profile => {
// Handle profile info here
});
该people.get方法让你多一点控制,并可能看起来像
const people = google.people({
version: "v1"
});
const fields = [
"names",
"emailAddresses",
"photos"
];
people.people.get({
resourceName: "people/me",
personFields: fields.join(',')
})
.then( user => {
// Handle the user results here
});