node.jsgoogle-apigoogle-plusgoogle-people-apiuserinfo

not able to get user info


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?


Solution

  • 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.