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

时间:2015-11-22 05:21:11

标签: facebook oauth yii2

目前正在使用Yii2框架并使用includable \yiisoft\yii2-authclient OAuth抽象类。我可以通过Facebook连接和验证,但无法弄清楚如何通过OAuth2范围配置选项访问可用的辅助数据。

相关但含糊不清(因为它没有解释范围如何适用于情况,也不解释如何使用authClient来检索数据:Login with Facebook API

配置

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

返回的对象如下:

yii\authclient\clients\Facebook Object
(
    [authUrl] => https://www.facebook.com/dialog/oauth
    [tokenUrl] => https://graph.facebook.com/oauth/access_token
    [apiBaseUrl] => https://graph.facebook.com
    [scope] => Array
        (
            [0] => email
            [1] => public_profile
            [2] => user_about_me
            [3] => user_location
            [4] => user_work_history
        )

    [attributeNames] => Array
        (
            [0] => name
            [1] => email
        )

    [version] => 2.0
    ...
)

如何访问用户的user_about_me数据?

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

2 个答案:

答案 0 :(得分:2)

您可以通过在attributeNames

中设置此值来获取以下信息
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',
                ],
            ],
        ],
    ],
    ...
],
...

重要链接和参考资料

https://developers.facebook.com/docs/facebook-login/permissions/v2.2

Login with Facebook API

What data can be obtained about a user who logs in with Facebook Oauth?

https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Did%2Cname%2Cemail&version=v2.7

https://developers.facebook.com/docs/graph-api/using-graph-api/#fieldexpansion

答案 1 :(得分:0)

您应该使用getUserAttributes方法:

public function actions()
{
    return [
        [
            'class' => 'yii\authclient\AuthAction',
            'successCallback' => [$this, 'successCallback']
        ]
    ];
}

/**
 * @param OAuth2 $client
 */
public function successCallback($client)
{
    $attributes = $client->getUserAttributes();
    ...
}