Using the Google+ API for PHP - you must get the email address of users

I am using the Google PHP API. The documentation is rather dull ... I want users to be able to connect their Google+ information and also use it to sign users in my database. To do this, I need to get the email that they use for their google account. I can’t figure out how to do this. It's easy enough on Facebook, causing permission when users connect to my app. Does anyone have an idea? This is the code I use to capture google + user profile, and it works fine, except that users may not have their email address.

include_once("$DOCUMENT_ROOT/../qwiku_src/php/google/initialize.php"); $plus = new apiPlusService($client); if (isset($_GET['code'])) { $client->authenticate(); $_SESSION['token'] = $client->getAccessToken(); header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); } if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); } if ($client->getAccessToken()) { $me = $plus->people->get('me'); print "Your Profile: <pre>" . print_r($me, true) . "</pre>"; // The access token may have been updated lazily. $_SESSION['token'] = $client->getAccessToken(); } else { $authUrl = $client->createAuthUrl(); print "<a class='login' href='$authUrl'>Connect Me!</a>"; } 

Without the users email address, this is a kind of defeat, allowing users to sign up for Google+. Does anyone more familiar with the Google API know how I can get it?

+4
source share
3 answers

The UserInfo API is now supported by the Google API API Client.

Here's a small sample application: http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/userinfo/index.php

An important sample bit is here:

  $client = new apiClient(); $client->setApplicationName(APP_NAME); $client->setClientId(CLIENT_ID); $client->setClientSecret(CLIENT_SECRET); $client->setRedirectUri(REDIRECT_URI); $client->setDeveloperKey(DEVELOPER_KEY); $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/plus.me')); // Important! $oauth2 = new apiOauth2Service($client); // Authenticate the user, $_GET['code'] is used internally: $client->authenticate(); // Will get id (number), email (string) and verified_email (boolean): $user = $oauth2->userinfo->get(); $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); print $email; 

I assume this fragment was called with a GET request, including an authorization code. Remember to enable or require apiOauth2Service.php for this to work. In my case it is something like this:

  require_once 'google-api-php-client/apiClient.php'; require_once 'google-api-php-client/contrib/apiOauth2Service.php'; 

Good luck.

+10
source

Updated API today:

 $googlePlus = new Google_Service_Plus($client); $userProfile = $googlePlus->people->get('me'); $emails = $userProfile->getEmails(); 

This implies the following:

  • You have verified the authenticity of the current user with the corresponding fields.
  • You configured $ client with your client_id and client_secret.
+7
source

Forgive me if I missed something, but how do you know which Google account to link them to if you do not have your email address? Typically, you prompt the user to enter their Google email address to find their profile first.

Using OAuth2, you can request permissions through the scope parameter. ( Documentation. ) I assume that the areas you want are https://www.googleapis.com/auth/userinfo.email and https://www.googleapis.com/auth/userinfo.profile .

Then, if you received an access token, just get profile information . (I assume that you were able to redeem the return authorization code for the access token?) Just make a request for receipt to https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken} , which returns an array of JSON profile data including email:

 { "id": "00000000000000", "email": " fred.example@gmail.com ", "verified_email": true, "name": "Fred Example", "given_name": "Fred", "family_name": "Example", "picture": "https://lh5.googleusercontent.com/-2Sv-4bBMLLA/AAAAAAAAAAI/AAAAAAAAABo/bEG4kI2mG0I/photo.jpg", "gender": "male", "locale": "en-US" } 

There must be a method in the PHP library to execute this request, but I cannot find it. There are no guarantees, but try the following:

 $url = "https://www.googleapis.com/oauth2/v1/userinfo"; $request = apiClient::$io->makeRequest($client->sign(new apiHttpRequest($url, 'GET'))); if ((int)$request->getResponseHttpCode() == 200) { $response = $request->getResponseBody(); $decodedResponse = json_decode($response, true); //process user info } else { $response = $request->getResponseBody(); $decodedResponse = json_decode($response, true); if ($decodedResponse != $response && $decodedResponse != null && $decodedResponse['error']) { $response = $decodedResponse['error']; } } } 

In any case, in the code you posted just pass the required scope to createAuthUrl() :

 else { $authUrl = $client->createAuthUrl("https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"); print "<a class='login' href='$authUrl'>Connect Me!</a>"; } 
+2
source

Source: https://habr.com/ru/post/1388993/


All Articles