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

194 views Asked by At

I have the following code snippet, to retrieve user information when he uses gmail to login:

 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. The user object which is retrieved is returning 'null', while it should return the user information object. Why is this so? Am i giving correct value for userId? How can i retrieve information like gmail address, first name, last name, etc.

1

There are 1 answers

0
Antonio Val On

It seems you are using the client wrong, here you have an example taken from the Node.js client repo:

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.

Did you check the err argument? I guess you are getting null because it is returning an error, something like you did not authenticate.