19
votes

i'm testing getting user information by google access token

http://www.mawk3y.net/glogin

after clicking sign in button i get redirected to

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=access_token_here

And get some JSON data like this

{
"issued_to": "my client id.apps.googleusercontent.com",
"audience": "my client id.apps.googleusercontent.com",
"user_id": "user id here",
"scope": "https://www.googleapis.com/auth/plus.login",
"expires_in": 3596,
"access_type": "online"
}

now i need to know how to extract user name , address and email any help please ?

thanks in advance

9

9 Answers

21
votes

Try this one:

 var url = 'https://www.googleapis.com/plus/v1/people/me?access_token={access_token}';

  $.ajax({
    type: 'GET',
    url: url,
    async: false,
    success: function(userInfo) {
      //info about user
      console.log(userInfo);
      console.log('test');
    },
    error: function(e) {
      console.log('error');

    }
  });
18
votes

You can verify the auth token received after google signin on your server using this api

Request

https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={accces_token}

Response

{

  "email_verified": "true",
  "email": "[email protected]",
  "name": "abhinav srivastava",
  "picture": "https://lh3.googleusercontent.com/-xgD_zFj1EgY/AAAAAAAAAAI/AAAAAAAACZ0/fnecSQ03o0Y/s96-c/photo.jpg",
  "given_name": "abhinav",
  "family_name": "srivastava",
  "locale": "en",
  ...
  ...
}

source

12
votes

I had the same issue. I wanted to extract the user information. But couldn't get the exact link to hit. Then I went through the code for Passport Google Strategy at Line number 54.

My scopes were ['profile', 'email']

GET Request

https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}

Response

{
  "sub": "23423....",
  "name": "John Doe",
  "given_name": "John",
  "family_name": "Doe",
  "picture": "<Profile picture URL>",
  "email": "[email protected]",
  "email_verified": true,
  "locale": "en"
}
3
votes

You need to use the access token (you get it in the redirect url) to access Google's People API. Check out the specs here.

You might find Google's OAuth2 playground pretty useful to get an idea of how to use access tokens to access Google's APIs.

Good luck!

2
votes

Check this. There is example with the demo

Code Snippet:

$accountObj = call_api($_SESSION['accessToken'],"https://www.googleapis.com/oauth2/v1/userinfo");

call_api calls the API and gets the data:

function call_api($accessToken,$url){
    $curl = curl_init($url);

    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    $curlheader[0] = "Authorization: Bearer " . $accessToken;
    curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);

    $json_response = curl_exec($curl);
    curl_close($curl);

    $responseObj = json_decode($json_response);

    return $responseObj;       
}

From the account object, the name can be accessed:

$your_name =  $accountObj->name;
2
votes

If you want token_info, pass tokeninfo as param

https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=ya29.a0AfH6SMArZZITzn-...

If you want all user info, pass userinfo as param

https://www.googleapis.com/oauth2/v3/userinfo?access_token=ya29.a0AfH6SMArZZITzn-...

tokeninfo reponse

{
    "azp": "",
    "aud": "",
    "sub": "",
    "scope": "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid",
    "exp": "",
    "expires_in": "",
    "email": "",
    "email_verified": "",
    "access_type": ""
}

userinfo reponse

{
    "sub": "",
    "name": "",
    "given_name": "",
    "family_name": "",
    "picture": "",
    "email": "",
    "email_verified": ,
    "locale": ""
}

Don't fogot to pass https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile as scope

var params = {
  'client_id': '',
  'redirect_uri': '',
  'response_type': 'token',
  'scope': 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
};
1
votes

You can use the quick start and see the sample here: sample
And here is a description of People:get

1
votes

This is a simple nodejs code at server side.

 var express = require('express');
 var appln = express();
 var google = require('googleapis');
 var plus = google.plus('v1');
 var OAuth = google.auth.OAuth2;
 var oauth2client = new OAuth(YOUR_CLIENT_ID  , YOUR_SECRET_ID ,  CALLBACK_REDIRECT_URI );

 appln.get("/tokens" , function(req , res ) {
         var code = req.query.code;
       oauth2client.getToken( code , function( err , tokens ){
                         if(err){
                            console.log(err);
                            res.send(err);
                            return;
                            }
                 oauth2client.setCredentials(tokens);
                 actoken = tokens.access_token;
                 reftoken = tokens.refresh_token;

       plus.people.get({  
                  userId: 'me',
                  auth: oauth2client
                  }, function (err, response) {
               // handle err and response
               var name = ""+response.displayName;
               var id =  ""+response.id;
               var age = ""+response.ageRange.min;
               if(err) console.log(err);
               console.log("Name : ", name," ",id,"",age);    
               res.send(response);

           });




        });
  });                       
0
votes

IF YOU ARE USING PHP

You can check here. In short, the code you are looking for is this

$payload = $client->verifyIdToken($id_token);

Here payload has the info you need, as long as you add the needed scopes.