我正在使用 yii2-authclient 为我的网站登录 Facebook,收到这样的回复
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] => *****)
这是我的配置
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'clientId' => '***',
'clientSecret' => '****',
],
],
]
能够获取用户名、用户 ID 但不能获取用户电子邮件 ID 为什么?我还应该做什么?
最佳答案
是的,终于得到了解决方案,
当您发出图形 API 请求时
$this->makeSignedRequest('me');
将其替换为
$this->makeSignedRequest('me?fields=id,name,email');
无论您是使用 facebook sdk,还是像我一样使用 yii2-eauth 扩展。
如果您使用的是 yii2-eauth,那么您需要在 vendor/nodge/yii2-eauth/src/services/FacebookOAuth2Service.php 中进行更改
然后添加此行以读取电子邮件属性
$this->attributes['email'] = $info['email'];
注意:我尝试了 yii 传递参数的方式,例如
$this->makeSignedRequest('me',array('fields'=>'id,email');
对我不起作用。
解决方案来源:
https://developers.facebook.com/docs/php/howto/example_retrieve_user_profile/5.0.0
关于Yii2 authclient 不返回用户的电子邮件 ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31377753/