Yii2 authclient 不返回用户的电子邮件 ID

Yii2 authclient not returning email id of user

我正在使用 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] => *****)

这是我的配置

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

        ],
    ],
]

我能够获取用户名、用户 ID 但不能获取用户电子邮件 ID,为什么?我还应该做什么?

    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).
'facebook' => [
    'class' => 'yii\authclient\clients\Facebook',
    'clientId' => '****',
    'clientSecret' => '*****',
 ],

够了。但有时您仍然无法获得电子邮件地址,因为某些 facebook 用户没有电子邮件地址:

  • Facebook 用户通过 phone
  • 注册
  • facebook 用户通过电子邮件注册,但电子邮件未验证

你必须抓住它才能通知用户。

我不知道还有没有其他例外。如果用户有电子邮件但您的应用程序无法获取该电子邮件地址,让我们检查 Facebook 开发人员 "status and review" 页面中列表 Approved Items 中的 email 权限以了解原因。 https://developers.facebook.com/apps/YOUR_APP_ID/review-status/

是的,终于找到解决方案了,

当你制作图 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/clients/Facebook.php 中有一个函数 initUserAttributes() 其中

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

替换为

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

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'];