0
votes

Google OAuth 2.0 Access Token's have an expiry time. I have integrated the Google Calendar API into my Ruby application however the problem is access token is expiring. How can I make the access token long lived one.

cal.login_with_refresh_token('ksdjkf_sdfkasdhfjhaskjdhfkasdhfkasjdlfasld')

{ "access_token" => "ksdjkf_sdfkasdhfjhaskjdhfkasdhfkasjdlfasld",

"token_type" => "Bearer",

"expires_in" => 2324

}

How to make this a long lived one which means untill unless he revokes access from his Google account user can be able to fetch his Calendar events.

1
@DaImTo made corrections thankssreenivas
Request offline access to get a refresh token and use the refresh token to get a new access token. Sorry I cant help with Ruby.DaImTo

1 Answers

1
votes

Here's how to create a long-lived OAuth token for the Google API:

  1. visit http://console.developers.google.com
  2. API Manager
  3. Credentials
  4. Create Credentials (OAuth client ID)
  5. Application type: Web Application
  6. Authorised redirect URIs: https://developers.google.com/oauthplayground
    • the resultant client ID / client secret is for your access token
  7. visit: https://developers.google.com/oauthplayground/
  8. Click the settings icon to show the OAuth 2.0 configuration
  9. Tick 'Use your own OAuth credentials'
  10. Enter the OAuth Client ID and OAuth Client secret that you have just created
  11. Check the entry for 'Calendar API v3' in the scopes field and click 'Authorize APIs'
  12. Click 'Allow'
  13. Click 'Exchange authorization code for tokens'
    • now you have a Refresh token and Access token for your client id / secret

Here's an example of using that oauth token with the ruby-api-client gem to talk to the Google Play API using signet. You should be able to modify this example to instead use the calendar API pretty easily:

https://gist.github.com/jkotchoff/e60fdf048ec443272045

Note this was inspired by: https://www.youtube.com/watch?v=hfWe1gPCnzc