paglahost
(David Hagen)
November 29, 2017, 10:17pm
1
Hi,
I can login using Google but there are no way to save the tokens. Shouldn’t yii\authclient\clients\Google have option to save tokens? Basically, I would like to access Gmail, Google drive etc from my own app. How do I save access_token,refresh_token after authentication with Google’s Oauth? I noticed the following things appear in $_SESSION.
$_SESSION = [
'__flash' => []
'__returnUrl' => '/myjobs/backend/web/'
'yii\\authclient\\clients\\Google_google_token' => yii\authclient\OAuthToken#1
(
[tokenParamKey] => 'access_token'
[tokenSecretParamKey] => 'oauth_token_secret'
[createTimestamp] => 1511989905
[yii\authclient\OAuthToken:_expireDurationParamKey] => 'expires_in'
[yii\authclient\OAuthToken:_params] => [
'access_token' => 'xxx'
'expires_in' => 3600,
'refresh_token' => 'xxx',
'id_token' => 'xxxx'
'token_type' => 'Bearer'
]
)
'__id' => 9
]
My partial code onAuthSuccess
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'onAuthSuccess'],
],
];
}
....
public function onAuthSuccess(ClientInterface $client){
$attributes=$client->getUserAttributes();
$mode = Yii::$app->getRequest()->getQueryParam('mode');
$attributes = $client->getUserAttributes();
$serviceId = $attributes['id'];
$serviceProvider = $client->getId();
$serviceTitle = $client->getTitle();
$firstname ='';
$lastname='';
$fullname ='';
*** how to access token?? token even doesn't exist in $client object**
}
egorsmkv
(Egorsmkv)
November 29, 2017, 11:24pm
2
The base class BaseOAuth.php has the method for determining an access token:
paglahost
(David Hagen)
November 30, 2017, 12:32am
3
Thank you. It’s abstract class. But how do you access it in the above case.
alirz23
(Ali Raza)
November 30, 2017, 7:26am
4
all the clients including google extend BaseAuth through Auth1 or Auth2, you should have access to getAccessToken did you try calling that method on $client
// try this
$client->getAccessToken();
// which should return
yii\authclient\OAuthToken
(
[tokenParamKey] => 'access_token'
[tokenSecretParamKey] => 'oauth_token_secret'
[createTimestamp] => 1511989905
[yii\authclient\OAuthToken:_expireDurationParamKey] => 'expires_in'
[yii\authclient\OAuthToken:_params] => [
'access_token' => 'xxx'
'expires_in' => 3600,
'refresh_token' => 'xxx',
'id_token' => 'xxxx'
'token_type' => 'Bearer'
]
)
you can call getToken to get the string token value
$client->getAccessToken()->getToken();
paglahost
(David Hagen)
December 1, 2017, 12:44am
5
Thank you so much. Now I can get access_token by $client->getAccessToken()->getToken().
How do I get refresh_token? I need these tokens to use Google web services ie Gmail to send email from my app.
And also is there anyway, automatically get access token when it’s expired? I tried setting ‘autoRefreshAccessToken’ in config file. But not working.
Sorry, too many questions.
Components in main-local.php
'class' => 'yii\authclient\Collection',
'clients' => [
'google' => [
'class' => 'yii\authclient\clients\Google',
'authUrl' => 'https://accounts.google.com/o/oauth2/auth?access_type=offline',
'clientId' => 'xxx',
'clientSecret' => 'xxx',
'autoRefreshAccessToken'=>true,
'scope'=>' https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/gmail.compose https://www.googleapis.com/auth/admin.directory.device.mobile.readonly',
//'scope'=>' https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/gmail.compose',
'returnUrl' => 'http://localhost/myjobs/backend/web/site/auth?authclient=google'
],
],
],
all the clients including google extend BaseAuth through Auth1 or Auth2, you should have access to getAccessToken did you try calling that method on $client
// try this
$client->getAccessToken();
// which should return
yii\authclient\OAuthToken
(
[tokenParamKey] => 'access_token'
[tokenSecretParamKey] => 'oauth_token_secret'
[createTimestamp] => 1511989905
[yii\authclient\OAuthToken:_expireDurationParamKey] => 'expires_in'
[yii\authclient\OAuthToken:_params] => [
'access_token' => 'xxx'
'expires_in' => 3600,
'refresh_token' => 'xxx',
'id_token' => 'xxxx'
'token_type' => 'Bearer'
]
)
you can call getToken to get the string token value
$client->getAccessToken()->getToken();