• Resolved eugene212

    (@eugene212)


    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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Beau Lebens

    (@beaulebens)

    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

    Thread Starter eugene212

    (@eugene212)

    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

    Thread Starter eugene212

    (@eugene212)

    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

    Plugin Author Beau Lebens

    (@beaulebens)

    Great, thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘OAuth token update’ is closed to new replies.