2015-12-09 82 views
2

我在我的應用程序中使用google authclient登錄.Google登錄正常工作,但我無法獲取用戶屬性。
web.phpYii2從google authclient登錄獲取用戶屬性

'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(); 
    } 

可以在任何一個可以幫助我實現這個..
謝謝。

回答

0

設置你的RETURNURL在web.php這樣的: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', 
    ], 
+0

雖然這個代碼片斷可以解決的問題,包括解釋[真幫助](//meta.stackexchange.com/q/114762),以提高您的文章的質量。請記住,你正在爲將來的讀者回答這個問題,而不僅僅是現在問的人!請編輯您的答案以添加解釋,並指出適用的限制和假設。 –