我使用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为什么?还有什么我应该做的?
答案 0 :(得分:3)
是的,终于得到了解决方案,
制作图表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
答案 1 :(得分:2)
我在上述解决方案的帮助下得到了解决方案。
在yii2-authclient / clients / Facebook.php中有一个函数initUserAttributes(),其中
return $this->api('me', 'GET');
替换为
return $this->api('me?fields=id,name,email', 'GET');
答案 2 :(得分:1)
yii2 eauth扩展确实存在一些错误。
首先在web.php中设置范围参数
'facebook' => array(
// register your app here: https://developers.facebook.com/apps/
'clientId' => '454208034652365',
'clientSecret' => '0e7161949129d4120cc5ac7d79f89098',
'class' => 'nodge\eauth\services\FacebookOAuth2Service',
'scope' => [
'email'
]
),
那么您可以像下面这样使用
$info = $eauth->makeSignedRequest('me', [
'query' => [
'fields' => join(',', [
'id',
'name',
'link',
'email'
])
]
]);
echo $info['email'];
答案 3 :(得分: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).
答案 4 :(得分:0)
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'clientId' => '****',
'clientSecret' => '*****',
],
这就够了。但有时候你仍然无法获得电子邮件地址,因为有些Facebook用户没有电子邮件地址:
你必须抓住它才能通知用户。
我不知道还有其他例外情况。如果用户有电子邮件但您的应用无法获取该电子邮件地址,请让我们检查列表email
中的Approved Items
权限状态并查看" Facebook开发者的页面知道原因。
https://developers.facebook.com/apps/YOUR_APP_ID/review-status/