node.js - 弃用 Google+

标签 node.js google-plus

我收到一封来自 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);
            })

最佳答案

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

但是,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 的网址,其中将包含一个名为 code 的参数。您将需要此代码来将其交换为凭据,包括身份验证 token

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

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

由于您似乎想要获取个人资料信息,因此您可以使用 userinfo 或 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
});

关于node.js - 弃用 Google+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54498023/

相关文章:

node.js - Node JS 中不使用 process.stdout.write 输出日志的方法?

javascript - Node.js - 监听监听器何时注册

node.js - 将音频文件转换为 16 位线性 PCM

android - Google+ 分享链接

ios - 返回应用页面,来自uiwebview

java - 从 Google Plus api 注销

javascript - __defineGetter__ 在 node.js 中被弃用了吗?

node.js - RxJS node-oauth 简单 get 请求失败

android - 尝试通过 YouTube API V3 访问正确的 YouTube 帐户

ios - google plus登录正在重定向到ios中的google.com