Google API Integration for Yii ¶
- Настройка компонента
- Проверка валидности текущего токена
Настройка компонента ¶
'components' => [
'google' => [
'class' => dicr\google\Google::class,
'clientConfig' => [
'client_id' => 'XXXXXXX.apps.googleusercontent.com',
'access_type' => 'offline',
'prompt' => 'select_account consent',
'client_secret' => 'XXXXXXXX',
'credentials' => 'xxx',
'scopes' => [
Google_Service_Sheets::SPREADSHEETS,
Google_Service_Sheets::DRIVE,
Google_Service_Sheets::DRIVE_FILE
],
'include_granted_scopes' => true
]
]
];
Проверка валидности текущего токена ¶
use Google\Client;
use yii\helpers\Url;
$google = Yii::$app->get('google');
$client = $google->client;
if ($client->isAccessTokenExpired() && !empty($client->getRefreshToken())) {
$token = $client->fetchAccessTokenWithRefreshToken();
if (!empty($token)) {
$this->module->api->sessionToken = $token;
}
}
if ($client->isAccessTokenExpired()) {
Yii::$app->user->returnUrl = Url::current();
$client->setRedirectUri(Url::to(['my-module/google-callback'], true));
return $this->redirect($client->createAuthUrl(), 303);
}
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.