Update scopes for calendar API, remove unnecessary logging

This commit is contained in:
2023-01-21 21:04:32 +01:00
parent 0a7a57affa
commit 9c9bf61c7e
4 changed files with 27 additions and 20 deletions

View File

@@ -19,26 +19,25 @@ class GoogleCalendarClient {
$this->client->setAccessType('offline'); $this->client->setAccessType('offline');
$this->client->setRedirectUri(url('/gcalendar/oauth2callback')); $this->client->setRedirectUri(url('/gcalendar/oauth2callback'));
$this->client->addScope(Google_Service_Calendar::CALENDAR); $this->client->addScope([Google_Service_Calendar::CALENDAR_READONLY , Google_Service_Calendar::CALENDAR_EVENTS_READONLY]);
// Set access toke on client if exist. // Set access toke on client if exist.
$accessToken = Settings::get('access_token'); $accessToken = Settings::get('access_token', []);
if (!empty($accessToken)) { if (!empty($accessToken)) {
$this->client->setAccessToken($accessToken); $this->client->setAccessToken($accessToken);
} }
// If there is no previous token or it's expired. // Ensure valid access token.
if ($tryRefreshToken && $this->client->isAccessTokenExpired()) { if ($tryRefreshToken && $this->client->isAccessTokenExpired()) {
// Refresh the token if possible.
if ($this->client->getRefreshToken()) { if (!empty($this->client->getRefreshToken())) {
$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken()); $this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
// Save new access token // Merge access token
Settings::set('access_token', $this->client->getAccessToken()); Settings::set('access_token', array_merge($accessToken, $this->client->getAccessToken()));
} else { } else {
Log::warning('G-Calendar: No valid access token given.'); Log::warning('G-Calendar: No valid refresh token given.');
} }
} }
} }
public function getClient() { public function getClient() {
@@ -49,5 +48,4 @@ class GoogleCalendarClient {
return new Google_Service_Calendar($this->client); return new Google_Service_Calendar($this->client);
} }
} }

View File

@@ -31,7 +31,7 @@ class GoogleCallback extends Controller {
$accessToken = $client->fetchAccessTokenWithAuthCode($code); $accessToken = $client->fetchAccessTokenWithAuthCode($code);
$client->setAccessToken($accessToken); $client->setAccessToken($accessToken);
Log::info('G-Calendar: Set access-token ['. print_r($client->getAccessToken(), true) .'].'); Log::info('G-Calendar: Set access_token ['. print_r($client->getAccessToken(), true) .'].');
Settings::set('access_token', $client->getAccessToken()); Settings::set('access_token', $client->getAccessToken());
return Redirect::to('/backend/system/settings/update/nicost/gcalendar/settings'); return Redirect::to('/backend/system/settings/update/nicost/gcalendar/settings');

View File

@@ -21,7 +21,6 @@ class OAuth extends FormWidgetBase {
$this->vars['redirectUrl'] = $client->getRedirectUri(); $this->vars['redirectUrl'] = $client->getRedirectUri();
$this->vars['isAccessTokenExpired'] = $client->isAccessTokenExpired() ? '1' : '0'; $this->vars['isAccessTokenExpired'] = $client->isAccessTokenExpired() ? '1' : '0';
$this->vars['clientIdExist'] = null != Settings::get('client_id', null) ? '1' : '0'; $this->vars['clientIdExist'] = null != Settings::get('client_id', null) ? '1' : '0';
$this->vars['accessToken'] = print_r(Settings::get('access_token'), true);
return $this->makePartial('oauth'); return $this->makePartial('oauth');
} }
@@ -30,28 +29,34 @@ class OAuth extends FormWidgetBase {
$class = new GoogleCalendarClient(true); $class = new GoogleCalendarClient(true);
$client = $class->getClient(); $client = $class->getClient();
$client->setPrompt('consent');
if ($client->isAccessTokenExpired()) { if ($client->isAccessTokenExpired()) {
// Request authorization from the user. // Request authorization from the user.
$authUrl = $client->createAuthUrl(); $authUrl = $client->createAuthUrl();
Log::info('G-Calendar: Request authorization with URL ['. $authUrl .'].'); //Log::info('G-Calendar: Request authorization with URL ['. $authUrl .'].');
$this->vars['auth_url'] = $authUrl; $this->vars['auth_url'] = $authUrl;
return $this->makePartial('gaccess'); return $this->makePartial('gaccess');
} }
Log::info('G-Calendar: Access token not expired.'); Flash::info(Lang::get('nicost.gcalendar::lang.message.accessTokenNotExpired'));
Flash::error(Lang::get('nicost.gcalendar::lang.message.accessTokenNotExpired'));
} }
public function onClearAccessToken() { public function onClearAccessToken() {
Settings::set('access_token', ''); Settings::set('access_token', []);
Flash::success(Lang::get('nicost.gcalendar::lang.message.accessTokenRemoved')); Flash::success(Lang::get('nicost.gcalendar::lang.message.accessTokenRemoved'));
$this->vars['isAccessTokenExpired'] = '1'; return $this->refreshTokenStatus();
}
private function refreshTokenStatus() {
$class = new GoogleCalendarClient(true);
$client = $class->getClient();
$this->vars['isAccessTokenExpired'] = $client->isAccessTokenExpired() ? '1' : '0';
return Redirect::refresh(); return Redirect::refresh();
} }
} }

View File

@@ -5,3 +5,7 @@
- Compatibility with October CMS 2 - Compatibility with October CMS 2
1.0.2: 1.0.2:
- Fix missing checkboxes at the calendar selector in the backend - Fix missing checkboxes at the calendar selector in the backend
1.1.0
- Update scopes for Calendar API to read only.
- Fix bug about missing refresh token after the update to the latest google clientapi version
- Remove unnecessary logging