Get user info via Google API

Can I get information from a user profile through the Google API? If possible, which API should I use?

I am interested in such information:

It would also be great to get other information from the user profile.

+63
oauth google-api userinfo
Aug 20 '11 at 8:21
source share
6 answers

Add this to the scope - https://www.googleapis.com/auth/userinfo.profile

And after authorization you will receive information from - https://www.googleapis.com/oauth2/v1/userinfo?alt=json

It has many downloads - including name, public profile, gender, photo, etc.

+82
Aug 21 '11 at 13:10
source share

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" } 

Tahiru Yasin:

This is an example php.
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']; 
+73
Nov 14 '11 at 9:17
source share

This area https://www.googleapis.com/auth/userinfo.profile is deprecated. Take a look at https://developers.google.com/+/api/auth-migration#timetable .

The new area that you will use to get profile information: profile or https://www.googleapis.com/auth/plus.login

and the endpoint is https://www.googleapis.com/plus/v1/people/ {userId} - userId may just be the "I" for the current user.

+25
Mar 17 '14 at 16:23
source share

I use PHP and solved it using version 1.1.4 google-api-php-client

Assuming the following code is used to redirect the 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 that a valid authentication code is returned in redirect_url , the following generates a token from the authentication code, and also provides 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, the location is not refundable. New YouTube Accounts Don't Have YouTube Usernames

+15
Jun 10 '15 at 13:15
source share

I use the Google APIs for .Net, but no doubt you can find the same way to get this information using a different version of the API. As indicated by user872858 , the userinfo.profile area is deprecated ( google article ).

To get information about the user profile, I use the following code (the rewritten part from the Google example ):

 IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow( new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = Secrets, Scopes = new[] { PlusService.Scope.PlusLogin,"https://www.googleapis.com/auth/plus.profile.emails.read" } }); TokenResponse _token = flow.ExchangeCodeForTokenAsync("", code, "postmessage", CancellationToken.None).Result; // Create an authorization state from the returned token. context.Session["authState"] = _token; // Get tokeninfo for the access token if you want to verify. Oauth2Service service = new Oauth2Service( new Google.Apis.Services.BaseClientService.Initializer()); Oauth2Service.TokeninfoRequest request = service.Tokeninfo(); request.AccessToken = _token.AccessToken; Tokeninfo info = request.Execute(); if (info.VerifiedEmail.HasValue && info.VerifiedEmail.Value) { flow = new GoogleAuthorizationCodeFlow( new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = Secrets, Scopes = new[] { PlusService.Scope.PlusLogin } }); UserCredential credential = new UserCredential(flow, "me", _token); _token = credential.Token; _ps = new PlusService( new Google.Apis.Services.BaseClientService.Initializer() { ApplicationName = "Your app name", HttpClientInitializer = credential }); Person userProfile = _ps.People.Get("me").Execute(); } 

Moreover, you can access almost anyone using userProfile.

UPDATE. To get this code, you must use the appropriate scope in the Google login button. For example, my button:

  <button class="g-signin" data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read" data-clientid="646361778467-nb2uipj05c4adlk0vo66k96bv8inqles.apps.googleusercontent.com" data-accesstype="offline" data-redirecturi="postmessage" data-theme="dark" data-callback="onSignInCallback" data-cookiepolicy="single_host_origin" data-width="iconOnly"> </button> 
+4
Jul 01 '14 at 12:30
source share

If you are in a client web environment, the new javascript auth2 API contains the much-needed getBasicProfile() function, which returns the username, email address, and image URL.

https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile

+1
Nov 02 '15 at 7:10
source share



All Articles