Question

I am trying to get the user profile details in Node.

The flow is such that I have a mobile app that generates the token for me (by authenticating through Google plus native SDK), then I send the token to my node server and I want to get the details about the user from Google. I tried the following (assuming I have the access_token from the client):

var request = require('request');

request('https://www.googleapis.com/oauth2/v2/userinfo?access_token=' + access_token, function(err, res, body) {
  if (err) return callback(err);

  if (res.statusCode != 200) {
    return callback(new Error('Invalid access token: ' + body));
  }
  else {
    var me;
    try { me = JSON.parse(body);}

    catch (e) {
      return callback(new Error('Unable to parse user data: ' + e.toString()));
    }

    console.log('user profile:', me);
  }
});

This works, but sometimes I get bad response like null displayName field, null email (even though I have the scope) and it's unreliable overall.

I have tried using Google API Node module but the flow there assumes I need to get token and is unnecessarily complicated.

Other than that, I don't understand the difference between Google's API versions and don't know when to use what.

EDIT

It seems that the problem with null displayName field only happens with tokens that were generated through the iOS SDK. Tokens generated with Android SDK are fine and calling the API with them resolves in displayName. I tried taking the iOS token and calling the following API :

https://www.googleapis.com/plus/v1/people/me?access_token=...

I got a displayName in the result, but after a couple of calls from my server I got

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }
}
Was it helpful?

Solution

The problem was isolated to tokens generated by my IOS users only.

It seems that the token I was getting through the IOS SDK was bad for using with the API call I mentioned in my question (https://www.googleapis.com/oauth2/v2/userinfo). Tokens generated by the Android SDK were working okay with the mentioned call.

So to find a common ground, I had to use the following API call from my server:

https://www.googleapis.com/plus/v1/people/me?access_token=...

You should get an object similar like this one (omitted the personal fields):

{
  kind:"plus#person",
  etag:"...",
  gender:"male",
  emails:[
    {
      value:"...",
      type:"account"
    }
  ],
  urls:[
    {
      value:"...",
      label:"Buzz"
    }
  ],
  objectType:"person",
  id:"...",
  displayName:"...",
  name:{
    familyName:"...",
    givenName:"..."
  },
  url:"...",
  image:{
    url:"..."
  },
  isPlusUser:true,
  language:"en",
  ageRange:{
    min:21
  },
  verified:false,
  cover:{
    layout:"banner",
    coverPhoto:{
      url:"...",
      height:626,
      width:940
    },
    coverInfo:{
      topImageOffset:0,
      leftImageOffset:0
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top