WPIntell

Source evidence

OAuth token update

Keyring · support · 2021-05-08T02:23:00+00:00

mixedsentiment
mediumseverity
0.84relevance
4replies
Evidence linked to opportunitycommercial context

Proof Health

Open evidence

Commercial opportunities need traceable source links before they are treated as build-worthy.

6 / 32 rows with source links

18.8% of this page's analysis has direct source links.

0 build-decision rows missing links

0 rows here require auditable proof before promotion.

26 rows with no attached evidence

0 rows have source counts but still need direct links.

Conversation

support
eugene212 resolved
Hi, Your plugin provides a solid framework to access APIs with OAuth and it helped me to connect to Google Calendar API, so I can now post events into calendar directly from my WP site, thank you ! Can you please clarify what happens when the acquired token expires (I could not find this information easily) ? Will it be automatically updated via “refresh token” or something else will be required ? Please advise, Eugene 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 Great, thank you!

Comments

4 shown
Beau Lebens 2021-05-09T16:23:00+00:00

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

eugene212 2021-05-09T21:47:00+00:00

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

eugene212 2021-05-09T22:02:00+00:00

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

Beau Lebens 2021-05-17T01:05:00+00:00

Great, thank you!