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

Php Yii2从google authclient登录获取用户属性,php,yii2,google-login,Php,Yii2,Google Login,我正在应用程序中使用google authclient登录。google登录工作正常,但我无法获取用户属性。 web.php 'google' => [ 'class' => 'yii\authclient\clients\GoogleOAuth', 'clientId' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'clientSecret' => 'xxxxxxxxxx

我正在应用程序中使用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',
        ],    
 public function actions()
{
    return [

        'auth' => [
            'class' =>'yii\authclient\AuthAction',
            'successCallback' => [
            $this, 'successCallback'
            ],
        ],
    ];
}  
sitecontroller.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',
        ],    
 public function actions()
{
    return [

        'auth' => [
            'class' =>'yii\authclient\AuthAction',
            'successCallback' => [
            $this, '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();
    }  
public函数successCallback($client)
{
$attributes=$client->getUserAttributes();
//打印($attributes);模具();
//这里有用户登录或注册
//打印($attributes['email']);死亡();
$user_email=$attributes['email'];
$user_name=$attributes['name'];
//echo$user_email;echo“
”; //echo$user\u name; //模具(); $user=user::find()->其中(['user\u email'=>$user\u email])->one(); $count=计数($user); 如果($count==0){ 返回$this->redirect(['user/create?email='.$user\u email.&name='.$user\u name.']); }否则{ 返回Yii::$app->user->login($user); //echo Yii::$app->user->identity->id;die(); }
有谁能帮我做到这一点吗?

谢谢..

在web.php中设置您的返回URL:

您的代码必须如下所示:

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

虽然此代码片段可能会解决问题,包括提高文章质量的说明。请记住,您是在将来为读者回答问题,而不仅仅是现在提问的人!请在回答时添加说明,并说明适用的限制和假设。