0

bellow is my node js script to get google user details using accessToken

 var accessToken = req.body.accessToken;
            console.log(accessToken)


            var google = require('googleapis');
            //google api
            var plus = google.plus('v1');
            var OAuth2 = google.auth.OAuth2;

            var oauth2Client = new OAuth2(
                config.google.clientID,
                config.google.clientSecret,
                config.google.redirect_uri
            );
            oauth2Client.setCredentials({access_token: accessToken});
            plus.people.get({
                userId: 'me',
                auth: oauth2Client
            }, function (err, response) {
                // handle err and response
                if (err) {
                    reject(err)
                } else {
                    console.log(response);
                    resolve(response)
                }
            });

need to get google login user details using accessToken. what is wrong in code?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
dev dev
  • 121
  • 1
  • 2
  • 17

2 Answers2

2

The most likely cause is the user in question has not created a google+ profile. Here are a few more options.

I am not sure what information you are trying to get but the best way to get user info is to authecate a user using the profile scope then request the data directly of the user info endpoint

Request

GET /userinfo/v2/me HTTP/1.1
Host: www.googleapis.com
Content-length: 0
Authorization: Bearer uzG4XqnvucBFk3jylgxnbtNhoOt2wCc3QvUcjk7PZhJ5m6G7ibtlBwbAQntJjJtLsxZfoDjhbASpzLmcFnlZ9o4qoMd2fCV2eRb4O5XrKRAXC

Response

{
  "family_name": "Lawton", 
  "name": "Linda Lawton", 
  "picture": "https://lh5.googleusercontent.com/-a1CWlFnA5xE/AAAAAAAAAAI/AAAAAAAAl1I/UcwPajZOuN4/photo.jpg", 
  "locale": "en", 
  "gender": "female", 
  "link": "https://plus.google.com/+LindaLawton", 
  "given_name": "Linda", 
  "id": "117200475532672775346"
}

You can also go though the google people api using the same profile scope

GET /v1/people/me HTTP/1.1
Host: people.googleapis.com
Content-length: 0
Authorization: Bearer NuzG4XqnvucBFk3jylgxnbtNhoOt2wCc3QvUcjk7PZhJ5m6G7ibtlBwbAQntJjJtLsxZfoDjhbASpzLmcFnlZ9o4qoMd2fCV2eRb4O5XrKRAXC

But this endpoint reads from Google+ so if the user has not filled anything out on their Google+ profile you wont see much data here.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • To clarify the Google People API which is different than the G+ People API, will work with non G+ users. It will return only public data by default. But you can get additional private data by requesting scopes such as as the `profile` scope. See [Documentation](https://developers.google.com/people/v1/how-tos/authorizing#profile-scopes) for other scopes you can request to get additional private data. The Google People API should return all of the data that the userinfo endpoint returns and more. – Amos Yuen Mar 29 '18 at 21:39
  • Google people API is actually linked to Google contacts and is not associated with Google plus however I suspect that it reads its initial data from Google plus when they initialize this new API a few years ago as the data apears the same but I haven't asked Google for verification of this theory its just a guess – Linda Lawton - DaImTo Mar 30 '18 at 06:37
  • @asmos I think if you read my answer it mentions using the profile scope – Linda Lawton - DaImTo Mar 30 '18 at 06:39
  • @DalmTo Right I saw that you mentioned the profile scope. The last line of your post "But this endpoint reads from Google+ so if the user has not filled anything out on their Google+ profile you wont see much data here." seems to imply that People API only reads from Google+. I'm clarifying that Google People API works with non G+ users and will read the same data that the userinfo endpoint reads and more (G+ profile, contacts, etc.) – Amos Yuen Mar 30 '18 at 20:37
2

You can use request module to get the user detail on your node server.


But Before requesting the user data, make sure you have authorized the API by giving it the desired scope. In your case, you need to give https://www.googleapis.com/auth/userinfo.profile in the scope.


When you receive your accessToken, use that token to call this google api

https://www.googleapis.com/oauth2/v1/userinfo 

const request = require('request');
// use any api you want to call.
request({
  url: 'https://www.googleapis.com/oauth2/v1/userinfo',
  method: 'GET',
  headers: {
    'Authorization': `Bearer ${YourAccessToken}`,
    'Accept': 'application/json'
  }
}, function(err, response, _user) {
  console.log('User Data', _user);
})

I hope this will solve your problem. If still there is some problem, you can test your Google APIs on OAuth 2.0 Playground

Amanpreet
  • 219
  • 3
  • 10