2015-07-13 24 views
3

現在用yii2-authclient的Facebook登錄我的網站,像這樣的Yii2 authclient

yii\authclient\clients\Facebook Object ([authUrl] => https://www.facebook.com/dialog/oauth?display=popup [tokenUrl] => https://graph.facebook.com/oauth/access_token [apiBaseUrl] => https://graph.facebook.com [scope] => email [version] => 2.0 [clientId] => *******[clientSecret] => ********[_returnUrl:yii\authclient\BaseOAuth:private] => http://www.usermodule.com/index.php?r=site%2Fauth&authclient=facebook [_curlOptions:yii\authclient\BaseOAuth:private] => Array () [_accessToken:yii\authclient\BaseOAuth:private] => yii\authclient\OAuthToken Object ([tokenParamKey] => access_token [tokenSecretParamKey] => oauth_token_secret [createTimestamp] => 1436772538 [_expireDurationParamKey:yii\authclient\OAuthToken:private] => [_params:yii\authclient\OAuthToken:private] => Array ([access_token] => ****************************** [expires] => 5182933)) [_signatureMethod:yii\authclient\BaseOAuth:private] => Array () [_id:yii\authclient\BaseClient:private] => facebook [_name:yii\authclient\BaseClient:private] => [_title:yii\authclient\BaseClient:private] => [_userAttributes:yii\authclient\BaseClient:private] => [_normalizeUserAttributeMap:yii\authclient\BaseClient:private] => [_viewOptions:yii\authclient\BaseClient:private] => [_events:yii\base\Component:private] => Array () [_behaviors:yii\base\Component:private] =>) Array ([name] => **** [id] => *****) 

這我得到響應不返回用戶的電子郵件ID是我的配置

'authClientCollection' => [ 
    'class' => 'yii\authclient\Collection', 
    'clients' => [ 
     'facebook' => [ 
      'class' => 'yii\authclient\clients\Facebook',    
      'clientId' => '***', 
      'clientSecret' => '****', 

     ], 
    ], 
] 

我能得到用戶名,用戶名,但不是用戶電子郵件ID爲什麼?我還應該做什麼?

回答

3

是終於得到了解決,

當您進行圖形API請求

$this->makeSignedRequest('me'); 

$this->makeSignedRequest('me?fields=id,name,email'); 

無論你使用Facebook的SDK或使用yii2-做更換像我這樣的eauth擴展。 如果您正在使用yii2-eauth那麼你需要在供應商改變這一/ nodge/yii2-eauth/src目錄/服務/ FacebookOAuth2Service.php

然後加入這一行閱讀電子郵件屬性

$this->attributes['email'] = $info['email']; 

注:我試圖通過爭論的方式警予像

$this->makeSignedRequest('me',array('fields'=>'id,email'); 

我沒有工作。

的解決方案來源: https://developers.facebook.com/docs/php/howto/example_retrieve_user_profile/5.0.0

+0

很容易修改傳遞參數,它會工作'$ info = $ this-> makeSignedRequest('me',array( 'query'=> array( 'fields'=>'email,name,birthday,gender,link ,first_name,last_name,age_range', ), ));' –

+0

何時調用$ this-> makeSignedRequest? sitecontroller? –

0
for using Yii2-authclient you have to configure(main.php) like that:- 
    inside components array write 
    'authClientCollection' => [ 
      'class' => 'yii\authclient\Collection', 
      'clients' => [ 
       'facebook' => [ 
        'class' => 'yii\authclient\clients\Facebook', 
        'clientId' => 'your Client-Id', 
        'clientSecret' => 'your Client-secret', 
       ], 

      ], 
     ], 
    in controller inside actions method write:- 
    auth' => [ 
       'class' => 'yii\authclient\AuthAction', 
       'successCallback' => [$this, 'successCallback'], 
       ], 

then create successCallback function like 
public function successCallback($client) 
     { 
      $attributes= $client->getUserAttributes(); 
      print_r($attributes); 
} 
then you got all attributes which are shared by Facebook(like email, firstname etc). 
+0

請再次閱讀我的問題取代它,我越來越從我在我的問題,但在已經共享Facebook的迴應我沒有得到電子郵件 – user3377747

+0

用給定的解決方案替換您的配置,然後檢查 – sprytechies

+0

完成!我得到的迴應是Array([name] => ****** [id] => *****)。沒有電子郵件ID – user3377747

0
'facebook' => [ 
    'class' => 'yii\authclient\clients\Facebook', 
    'clientId' => '****', 
    'clientSecret' => '*****', 
], 

這就足夠了。但有時,你仍然無法獲得的電子郵件地址,因爲一些Facebook的用戶不具有的電子郵件地址:電話

  • Facebook的用戶註冊
  • 通過電子郵件,但是電子郵件
  • Facebook的用戶註冊未驗證

你必須趕上它來通知用戶。

我不知道有其他例外。如果用戶有電子郵件,但您的應用程序無法獲取該電子郵件地址,請在Facebook開發者的「狀態和評論」頁面中檢查email許可Approved Items以瞭解原因。 https://developers.facebook.com/apps/YOUR_APP_ID/review-status/

+0

我有通過電子郵件ID的fb帳戶,我測試與我的朋友帳戶也沒有相同的電子郵件作爲迴應。覈准項目電子郵件,公開個人資料,user_friends顯示默認爲已批准。 – user3377747

2

我上面的解決方案的幫助下解決方案。

在yii2-authclient/clients/Facebook中。PHP有一個函數initUserAttributes(),其中

return $this->api('me', 'GET'); 

return $this->api('me?fields=id,name,email', 'GET');