【问题标题】:Deprecation of Google plus弃用 Google plus
【发布时间】:2019-02-04 11:01:06
【问题描述】:

我收到一封来自 Google 的电子邮件,说所有 Google+ API 的使用都将被关闭。我目前使用 googleAPI.google.plus 让人们使用 Google 登录。此插件是否会添加更新以支持向 Google 授权用户的新方式?

环境细节: 操作系统: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);
            })

【问题讨论】:

标签: node.js google-plus


【解决方案1】:

您没有展示您如何使用该对象进行登录,因此有点难以回答。

但是,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。一旦他们登录到该 URL,他们将被重定向到您指定为 YOUR_REDIRECT_URL 的 URL,其中将包含一个名为 code 的参数。您将需要此代码来交换凭据,包括身份验证令牌

const {tokens} = await oauth2Client.getToken(code)
oauth2Client.setCredentials(tokens);

如果您只需要使用API Key(这是您的示例所暗示的),那么您只需像现在对您需要进行的 API 调用一样包含密钥。但这与授权无关。

由于您似乎想要获取个人资料信息,因此您可以使用 userinfo 或 People API 之类的内容,然后选择您希望用户使用的字段。

使用 userinfo 可能看起来像

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
});

【讨论】:

  • 感谢您的帮助!您应该添加我的登录方式。刚刚更新了问题以包含该问题。因此,一旦我获得他们的令牌,我就会使用 plus.people.get 来查看其是否有效并从谷歌获取有关该用户的信息。这个 api 显然被谷歌关闭了。为了继续能够做到这一点,我需要如何改变它?
猜你喜欢
  • 1970-01-01
  • 2019-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 2016-02-21
相关资源
最近更新 更多