Abhishek saini
Abhishek saini

Reputation: 527

how to get people info using google api in node js?

I want to verify user google id on server side.So i want user google info by using google api.I have gone through all documentation but i stuck.I have seen this code,its working fine:

var google = require('googleapis');
var plus = google.plus('v1');
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

// Retrieve tokens via token exchange explained above or set them:
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
});

but this is for google plus.I want to fetch user google profile with id not google plus info. Please help.

Upvotes: 3

Views: 3577

Answers (1)

irtaza
irtaza

Reputation: 749

For googleapis version 19.0.0, you're gonna have to do something like this:

const googleapis = require('googleapis');

googleapis.people('v1').people.get({
  resourceName: 'people/me',
  personFields: 'emailAddresses,names',
  auth: oauth2Client,
  (err, response) => {
    // do your thing
  }
});

The fields resourceName and personFields are the ones that you might get error for saying that these are mandatory. You can find details on these here https://developers.google.com/people/api/rest/v1/people/get.

As for the scopes, following should be enough for this code snippet: https://www.googleapis.com/auth/userinfo.email and https://www.googleapis.com/auth/userinfo.profile

Upvotes: 1

Related Questions