[Oauth] Get user info via Google API


Answers

scope - https://www.googleapis.com/auth/userinfo.profile

return youraccess_token = access_token

get https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token

you will get json:

{
 "id": "xx",
 "name": "xx",
 "given_name": "xx",
 "family_name": "xx",
 "link": "xx",
 "picture": "xx",
 "gender": "xx",
 "locale": "xx"
}

To Tahir Yasin:

This is a php example.
You can use json_decode function to get userInfo array.

$q = 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=xxx';
$json = file_get_contents($q);
$userInfoArray = json_decode($json,true);
$googleEmail = $userInfoArray['email'];
$googleFirstName = $userInfoArray['given_name'];
$googleLastName = $userInfoArray['family_name'];
Question

Is it possible to get information from user's profile via Google API? If it is possible, which API should I use?

I'm interesting in such information:

Also it would be cool to get other information from user's profile.




Which Google api to use for getting user's first name, last name, picture, etc?

The contacts API perhaps works, but you have to request permission from the user to access all contacts. If I were a user, that would make me wary of granting the permission (because this essentially gives you permission to spam all my contacts...)

I found the response here to be useful, and only asks for "basic profile information":

Get user info via Google API

I have successfully used this approach, and can confirm it returns the following Json object:

{
  "id": "..."
  "email": "...",
  "verified_email": true,
  "name": "....",
  "given_name": "...",
  "family_name": "...",
  "link": "...",
  "picture": "...",
  "gender": "male",
  "locale": "en"
}






I'm using PHP and solved this by using version 1.1.4 of google-api-php-client

Assuming the following code is used to redirect a user to the Google authentication page:

 $client = new Google_Client();
 $client->setAuthConfigFile('/path/to/config/file/here');
 $client->setRedirectUri('https://redirect/url/here');
 $client->setAccessType('offline'); //optional
 $client->setScopes(['profile']); //or email
 $auth_url = $client->createAuthUrl();
 header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
 exit();

Assuming a valid authentication code is returned to the redirect_url, the following will generate a token from the authentication code as well as provide basic profile information:

 //assuming a successful authentication code is return
 $authentication_code = 'code-returned-by-google';
 $client = new Google_Client();
 //.... configure $client object code goes here
 $client->authenticate($authentication_code);
 $token_data = $client->getAccessToken();

 //get user email address
 $google_oauth =new Google_Service_Oauth2($client);
 $google_account_email = $google_oauth->userinfo->get()->email;
 //$google_oauth->userinfo->get()->familyName;
 //$google_oauth->userinfo->get()->givenName;
 //$google_oauth->userinfo->get()->name;
 //$google_oauth->userinfo->get()->gender;
 //$google_oauth->userinfo->get()->picture; //profile picture

However, location is not returned. New YouTube accounts don't have YouTube specific usernames




How to get the profile picture of my Gmail account?

This api https://www.googleapis.com/plus/v1/people/me returns

{
 "gender": "male",
 "id": "234343028403284",
 "displayName": "xxx",
 "name": {
    "familyName": "foo",
    "givenName": "bar"
   },
 "url": "https://plus.google.com/34324234324324",
 "image": {
  "url": "https://lh3.googleusercontent.com/dlkfjdsfds/djfldsf"
   }
 ...
 }

Before your application can get access to data from a user's Google Account, the application must request authorization from the user.

See https://developers.google.com/+/api/oauth for information about oauth.

try: https://developers.google.com/oauthplayground/ will help you understand oauth flow: