Assuming you extended the Google Base service to communicate with GCal, and specifically using the ::request()
, then it will attempt to automatically refresh the token whenever it’s close to expiry. That will mean that some of your requests might appear to take longer, because an additional request needs to happen to refresh the token first. Basically though, you shouldn’t need to think about it
Hi Beau,
Thanks for the clarification !
For new ‘google-calendar’ extension I have used as a template already existing ‘google-mail’ and successfully connected to my Google Calendar, so it’s now “set-and-forget” case for me since the code to call API is, indeed, using ::request:
// prepare Google Calendar API service access
$service = Keyring::get_service_by_name("google-calendar");
$token = current($service->get_tokens());
$service->set_token($token);
// ========================= SEND REQUESTS =========================
// Calendar server
{
$api_response = $service->request($cal_url, $cal_args);
error_log (serialize ($api_response));
}
Regards,
Eugene
Hi Beau,
Please find below the code for ‘google-calendar’ events extension if you wish to add it to Keyring:
<?php
/**
* Google Calendar service definition for Keyring.
*
* Google Calendar API: https://developers.google.com/calendar/v3/reference/
* OAuth implementation: https://developers.google.com/identity/protocols/OAuth2WebServer
* App registration: https://console.developers.google.com/
*/
class Keyring_Service_GoogleCalendar extends Keyring_Service_GoogleBase {
const NAME = 'google-calendar';
const LABEL = 'Google Calendar';
// See https://developers.google.com/identity/protocols/googlescopes
const SCOPE = 'https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/userinfo.profile';
const ACCESS_TYPE = 'offline';
function __construct() {
parent::__construct();
}
function _get_credentials() {
if (
defined( 'KEYRING__GOOGLECALENDAR_KEY' )
&&
defined( 'KEYRING__GOOGLECALENDAR_SECRET' )
) {
return array(
'redirect_uri' => defined( 'KEYRING__GOOGLECALENDAR_URI' ) ? constant( 'KEYRING__GOOGLECALENDAR_URI' ) : '', // optional
'key' => constant( 'KEYRING__GOOGLECALENDAR_KEY' ),
'secret' => constant( 'KEYRING__GOOGLECALENDAR_SECRET' ),
);
} else {
return null;
}
}
}
add_action( 'keyring_load_services', array( 'Keyring_Service_GoogleCalendar', 'init' ) );
Regards,
Eugene