Yii2从google authclient登录获取用户属性

时间:2015-12-09 07:13:58

标签: php yii2 google-login

我在我的应用程序中使用google authclient登录.Google登录工作正常,但我无法获取用户属性。
web.php

 'google' => [
            'class' => 'yii\authclient\clients\GoogleOAuth',
            'clientId' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            'clientSecret' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
            'scope'=>' https://www.googleapis.com/auth/plus.profile.emails.read',
            'returnUrl'=>'https://localhost/mysite',
        ],    

sitecontroller.php

 public function actions()
{
    return [

        'auth' => [
            'class' =>'yii\authclient\AuthAction',
            'successCallback' => [
            $this, 'successCallback'
            ],
        ],
    ];
}  

successcallback()

public function successCallback($client)
{

    $attributes = $client->getUserAttributes();
    // print_r($attributes);die();
    // user login or signup comes here
   // print_r($attributes['email']);die();
    $user_email = $attributes['email'];
    $user_name = $attributes['name'];
    // echo $user_email;echo '<br>';
    // echo $user_name;
    // die();

    $user = User::find()->where(['user_email'=>$user_email])->one();
    $count = count($user);
    if ($count == 0 ){
        return $this->redirect(['user/create?email='.$user_email.'&name='.$user_name.'']);
    }else{
        return Yii::$app->user->login($user);
        //echo Yii::$app->user->identity->id;die();
    }  

任何人都可以帮我实现这个目标。
谢谢..

1 个答案:

答案 0 :(得分:0)

在web.php中设置returnUrl:https://test.com/site/auth?authclient=google

您的代码必须如下所示:

 'google' => [
        'class' => 'yii\authclient\clients\Google',
        'clientId' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'clientSecret' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
        'returnUrl'=>'https://test.com/site/auth?authclient=google',
    ],