简体   繁体   中英

Google+ sign-in, How to get user email

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

( Google_Service_Oauth2_Userinfoplus )[ 268 ] Google_Service_Oauth2_Userinfoplus )[ 268 ]
protected 'internal_gapi_mappings' => (size=3) (大小= 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' =>
---- (size=2) (大小= 2)
------'given_name' => string 'SomeFirstName' (length=4)
------'family_name' => string 'SomeFamilyName' (length=6)
-- protected 'processed' =>
---- (size=0) (大小= 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 :).

So I figured out where the error came from.

In my code, google are not sending me the user's email because my application is not authentified. Because of this authentification problem, I could have access only to the user's public information but not the email.

This authentification error came from this line in the HTML

data-redirecturi="postmessage"

When the value "postmessage" is used as redirect_uri, your application will have "guest" privilege and google will not throw any error regarding the redirect_uri parameter. This value should only be used in early developement to test the communication between google's api and your application.

Here is the official documentation about redirect_uri parameter:

The value of this parameter must exactly match one of the values that appear in the Credentials page in the Google Developers Console (including the http or https scheme, case, and trailing slash)

However, I was not able to make things works using google javascript SDK as shown above. I used a basic link :

<a href="https://accounts.google.com/o/oauth2/auth?redirect_uri=http://www.example.com/googleplus/&response_type=code&client_id={{ google.cid }}&scope=https://www.googleapis.com/auth/plus.login+https://www.googleapis.com/auth/userinfo.email&access_type=offline" > 
  Connect with Google+ 
</a>

The oAuth Playground helped me a lot to solve this problem.

Hope it'll help someone :)

You need to set the scope for email in html too, more information email scopes :

<div id="signinButton">
  <span class="g-signin"
    data-scope="https://www.googleapis.com/auth/plus.login email"
    ...
    data-callback="signInCallback">
  </span>
</div>

or the other option:

<div id="signinButton">
  <span class="g-signin"
    data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read"
    ...
    data-callback="signInCallback">
  </span>
</div>

go to Google/model.php cheange

protected $modelData = array();
to
public $modelData = array();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM