我在網頁上實現了一個按鈕「在Google+上登錄」,除了我無法獲取用戶的電子郵件地址,一切正常。Google+登錄,如何獲取用戶電子郵件
這裏是我的HTML代碼
<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>
當此按鈕,用戶點擊&接受我的申請分享他的數據,一碼是AJAX發送到服務器。這是應該獲取用戶數據的服務器部分。
$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));
這裏是用戶信息的的var_dump
對象( Google_Service_Oauth2_Userinfoplus)]
保護 'internal_gapi_mappings'= > 陣列 (大小= 3)
- 'familyName'= >字符串 'FAMILY_NAME' (長度= 11)
- '給定名稱'= >字符串 'GIVEN_NAME' (長度= 10)
- 'verifiedEmail'= >串'verified_email' (長度= 14)
公共 '電子郵件'= >空
公共 'familyName'= >字符串> 'SomeFamilyName' (長度= 6)
公共 '性別'= >串 '男性' (長度= 4)
公共 '給定名稱'= >字符串 'SomeFirstName' (長度= 4)
公共 'HD' = >空
公共的 'id'= >字符串 '1021714674404 ********' (長度= 21)
公共 '鏈接'= >字符串 'https://plus.google.com/1021714674404********' (長度= 45)
公共 '區域設置'= >串成 'en' (長度= 2)
公共 '姓名'= >字符串 'SomeFirstName SomeFamilyName' (長度= 11)
公共 '畫面'= >串的「https://lh6.googleusercontent。COM/someUrl /」 (長度= 92)
公共 'verifiedEmail'= >空
- 保護 'modelData'= >
---- 陣列 (大小= 2)
------ 'GIVEN_NAME'= >字符串 'SomeFirstName' (長度= 4)
------ 'FAMILY_NAME'= >字符串 'SomeFamilyName' (長度= 6)
- 保護 '處理'= >
---- 陣列 (大小= 0)
------ 空
正如你可以看到,我的電子郵件地址是NULL,我真的需要至少獲得電子郵件地址(famillyName和firstname,但我有這些)。
在beggining我用Google_Service_Plus對象,但我有同樣的結果(電子郵件空),我發現這個線程(Google Apps SSO - First and Last Name with OAuth 2 without using Google+)目前的解決方案,但它仍然沒有工作 我也發現這個線程(Using Google+ API for PHP -- Need to get the users email),但似乎現在將apiOauth2Service稱爲Google_Service_Oauth2,因此它基本相同。
奇怪的是,當我使用谷歌自己的例子(https://developers.google.com/apis-explorer/#p/plus/v1/plus.people.get),我嘗試用我的用戶ID得到我的電子郵件地址,它返回一個空數組(它解釋了我的代碼中的空值)。
200個OK
- 隱藏標題 -
的Cache-Control:私人,最大年齡= 0,必重新驗證,無轉換 內容編碼:gzip 內容 - 長度:23 內容類型:application/json;字符集= UTF-8 日期:星期一,2014年11月24日17時34分39秒GMT 的Etag: 「RqKWnRU4WW46-6W3rWhLR9iFZQM/C5t5kUwLc-tXgfm2-1GGe31qPDs」 到期:星期一,2014年11月24日17時34分39秒GMT 服務器: GSE
{}
我也試過這個解決方案(Google Api get users Email Address),但它也不起作用。
但是,當我嘗試使用我的Google帳戶註冊到此網站(https://www.airbnb.fr/)時,他們得到了我的電子郵件地址!所以這是可能的,我只是不知道如何。
在此先感謝您的幫助:)。
不幸的是不斷變化的範圍,因爲它來自於redirectUri不能解決問題 – 2014-12-01 10:03:09