繁体   English   中英

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

[英]Yii2 get user attributes from google authclient 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();
    }  

谁能帮助我实现这一目标。
谢谢..

在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',
    ], 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM