[英]Yii2 get user attributes from google authclient login
I am using google authclient login in my application.Google login is working perfectly but I am unable to get the user attributes. 我在我的应用程序中使用google authclient登录。Google登录运行正常,但是无法获取用户属性。
web.php 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 sitecontroller.php
public function actions()
{
return [
'auth' => [
'class' =>'yii\authclient\AuthAction',
'successCallback' => [
$this, 'successCallback'
],
],
];
}
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();
}
Could any one help me to achieve this.. 谁能帮助我实现这一目标。
Thank you.. 谢谢..
set your returnUrl in web.php this : https://test.com/site/auth?authclient=google 在web.php中设置您的returnUrl: https ://test.com/site/auth?authclient = google
your code must be like below : 您的代码必须如下所示:
'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.