donglu9978 2014-11-25 13:42
浏览 211
已采纳

Google+登录,如何获取用户电子邮件

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 :).

  • 写回答

3条回答 默认 最新

  • dongyuli4538 2014-12-01 10:33
    关注

    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 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)