I'm implementing a button "Sign-in with Google+" on a web page and everything works fine except that I can't get the user email address.
Here is my HTML code
<div id="signinButton">
<span class="g-signin"
data-scope="https://www.googleapis.com/auth/plus.login"
data-clientid="{{ google.cid }}"
data-redirecturi="postmessage"
data-accesstype="offline"
data-cookiepolicy="single_host_origin"
data-callback="signInCallback">
</span>
</div>
When the user click on this button & accept to share his data with my application, a code is send in ajax to the server. Here is the server part that is supposed to get the user data.
$code = $request->getContent();
$client = new Google_Client();
$client->setClientId('Google_Api_Code');
$client->setClientSecret('Google_Api_Secret');
$client->setRedirectUri('postmessage');
$client->setScopes('email', 'profile');
$client->authenticate($code);
$oAuth = new Google_Service_Oauth2($client);
$oAttr = $oAuth->userinfo->get();
return new Response(var_dump($oAttr));
And here is the var_dump of the userinfo
object(Google_Service_Oauth2_Userinfoplus)[268]
protected 'internal_gapi_mappings' => array (size=3)
--'familyName' => string 'family_name' (length=11)
--'givenName' => string 'given_name' (length=10)
--'verifiedEmail' => string 'verified_email' (length=14)
public 'email' => null
public 'familyName' => string > 'SomeFamilyName' (length=6)
public 'gender' => string 'male' (length=4)
public 'givenName' => string 'SomeFirstName' (length=4)
public 'hd' => null
public 'id' => string '1021714674404********' (length=21)
public 'link' => string 'https://plus.google.com/1021714674404********' (length=45)
public 'locale' => string 'en' (length=2)
public 'name' => string 'SomeFirstName SomeFamilyName' (length=11)
public 'picture' => string 'https://lh6.googleusercontent.com/someUrl/' (length=92)
public 'verifiedEmail' => null
--protected 'modelData' =>
----array (size=2)
------'given_name' => string 'SomeFirstName' (length=4)
------'family_name' => string 'SomeFamilyName' (length=6)
--protected 'processed' =>
----array (size=0)
------empty
As you can see, my email address is NULL and I really need to get at least the email address (famillyName and firstname too but i have those).
At the beggining I was using Google_Service_Plus object but I had the same result (email null) and I found the current solution on this thread (Google Apps SSO - First and Last Name with OAuth 2 without using Google+) but it still not working I also found this thread (Using Google+ API for PHP -- Need to get the users email) but the apiOauth2Service seems to now be called Google_Service_Oauth2 so it's basically the same.
What is strange is that when I use Google own example (https://developers.google.com/apis-explorer/#p/plus/v1/plus.people.get) and I try to get my emails address with my user id, it returns a blank array (which explain the null value in my code).
200 OK
- Hide headers -
Cache-Control: private, max-age=0, must-revalidate, no-transform Content-Encoding: gzip Content-Length: 23 Content-Type: application/json; charset=UTF-8 Date: Mon, 24 Nov 2014 17:34:39 GMT Etag: "RqKWnRU4WW46-6W3rWhLR9iFZQM/C5t5kUwLc-tXgfm2-1GGe31qPDs" Expires: Mon, 24 Nov 2014 17:34:39 GMT Server: GSE
{ }
I also tried this solution (Google Api get users Email Address) but it doesn't work either.
However, when I try to register to this website (https://www.airbnb.fr/) with my google account, they got my email address ! So it is possible, I just don't know how.
Thanks in advance for your help :).