Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
成功验证后,使用Yi2 authClient从Facebook检索范围数据_Facebook_Oauth_Yii2 - Fatal编程技术网

成功验证后,使用Yi2 authClient从Facebook检索范围数据

成功验证后,使用Yi2 authClient从Facebook检索范围数据,facebook,oauth,yii2,Facebook,Oauth,Yii2,目前正在使用Yii2框架并使用可包含的\yiisoft\yii2authclientOAuth抽象类。我可以通过Facebook进行连接和身份验证,但无法确定如何通过OAuth2范围配置选项访问可用的辅助数据 相关但模糊(因为它没有解释范围如何应用于情况,也没有解释如何使用authClient检索数据: 配置 'authClientCollection' => [ 'class' => 'yii\authclient\Collection', 'clients' =&

目前正在使用Yii2框架并使用可包含的
\yiisoft\yii2authclient
OAuth抽象类。我可以通过Facebook进行连接和身份验证,但无法确定如何通过OAuth2范围配置选项访问可用的辅助数据

相关但模糊(因为它没有解释范围如何应用于情况,也没有解释如何使用authClient检索数据:

配置

'authClientCollection' => [
    'class' => 'yii\authclient\Collection',
    'clients' => [
        'facebook' => [
            'authUrl'      => 'https://www.facebook.com/dialog/oauth',
            'class'        => 'yii\authclient\clients\Facebook',
            'clientId'     => '*****',
            'clientSecret' => '*****',
            'scope'        => [
                'email', 
                'public_profile', 
                'user_about_me', 
                'user_location', 
                'user_work_history',
            ]
        ],
    ],
],
控制器设置:

public function actions()
{
    return [
        'auth' => [
            'class'           => 'yii\authclient\AuthAction',
            'successCallback' => [$this, 'onAuthSuccess'],
        ],
    ];
}
...
/**
 * [onAuthSuccess description]
 *
 * @param  [type] $client [description]
 * @return [type]         [description]
 */
public function onAuthSuccess($client)
{
    $attributes = $client->getUserAttributes();

    echo '<pre>';
    print_r( $attributes );
    echo '</pre>';
    exit;
...
如何访问用户的
user\u about\u me
数据


*编辑以添加提供数据转储的控制器逻辑。

您应该使用
getUserAttributes
方法:

id
name
first_name
last_name
age_range
link
gender
locale
picture
timezone
updated_time
verified

通过在
attributeNames

...
'components' => [
    ...
    'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
            'facebook' => [
                'class' => 'yii\authclient\clients\Facebook',
                'authUrl' => 'https://www.facebook.com/dialog/oauth',
                'clientId' => 'YOUR APP CLIENT ID',
                'clientSecret' => 'YOUR APP CLIENT SECRET',
                'attributeNames' => [
                    'id',
                    'name',
                    'first_name',
                    'last_name',
                    'link',
                    'about',
                    'work',
                    'education',
                    'gender',
                    'email',
                    'timezone',
                    'locale',
                    'verified',
                    'updated_time',
                ],
            ],
        ],
    ],
    ...
],
...
在您的配置文件中

重要链接和参考资料


$client->getUserAttributes()只返回以下内容:数组([name]=>******[email]=>******[id]=>*****)将诸如
user\u about\u me
之类的作用域作为参数传递,将返回相同的数组。是的,在对不同的OAuth提供程序进行了更多的挖掘和处理后,我们最终实现了我们想要的工作方式。虽然OAuth是一个行业标准流程,但每个提供程序都以不同的方式实现它,这是一件非常痛苦的事情。:S
id
name
first_name
last_name
age_range
link
gender
locale
picture
timezone
updated_time
verified
...
'components' => [
    ...
    'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
            'facebook' => [
                'class' => 'yii\authclient\clients\Facebook',
                'authUrl' => 'https://www.facebook.com/dialog/oauth',
                'clientId' => 'YOUR APP CLIENT ID',
                'clientSecret' => 'YOUR APP CLIENT SECRET',
                'attributeNames' => [
                    'id',
                    'name',
                    'first_name',
                    'last_name',
                    'link',
                    'about',
                    'work',
                    'education',
                    'gender',
                    'email',
                    'timezone',
                    'locale',
                    'verified',
                    'updated_time',
                ],
            ],
        ],
    ],
    ...
],
...