简体   繁体   English

无法通过Google Plus登录API + Node.js获取用户信息

[英]Not getting user information with Google Plus sign-in api + Node.js

I have the following code snippet, to retrieve user information when he uses gmail to login: 我有以下代码片段,以在他使用gmail登录时检索用户信息:

 oauth2Client.getToken(code, function(err, tokens) {
        if (err) {
            console.log(err);
            res.send(err);
            return;
        }

        console.log("allright!!!!");

        var plus = google.plus('v1');

        var API_KEY = 'AXXXXXXXXXXXXXXXXXXXXXXXXXU'; // specify your API key here

        plus.people.get({
            auth: API_KEY,
            userId: 'me'
        }, function (err, user) {
            console.log(user);
        });

        res.send(tokens);
    });

Gmail and google+ APIs are both enabled. Gmail和google + API均已启用。 The user object which is retrieved is returning 'null', while it should return the user information object. 检索到的用户对象返回“ null”,而它应该返回用户信息对象。 Why is this so? 为什么会这样呢? Am i giving correct value for userId? 我是否为userId提供正确的值? How can i retrieve information like gmail address, first name, last name, etc. 我该如何检索Gmail地址,名字,姓氏等信息。

It seems you are using the client wrong, here you have an example taken from the Node.js client repo : 似乎您使用的客户端不正确,这里有一个示例取自Node.js客户端存储库

oauth2Client.setCredentials({
  access_token: 'ACCESS TOKEN HERE',
  refresh_token: 'REFRESH TOKEN HERE'
});

plus.people.get({
  userId: 'me',
  auth: oauth2Client
}, function (err, response) {
  // handle err and response
});

You have to pass the oauth2Client instance through the auth property, not API_KEY . 您必须通过auth属性(而不是API_KEY传递oauth2Client实例。

Did you check the err argument? 您检查了err参数吗? I guess you are getting null because it is returning an error, something like you did not authenticate. 我猜您正在获取null,因为它返回了错误,例如您未通过身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM