I have a rails app that is using the gem 'omniauth-google-oauth2'. Everything is working so far. Now I am trying to pull a users calendar after they link their Google account.
I have followed several tutorials, but it seems that I am not able to pull the calendar information. All the tutorials used:
gem 'google-api-client', '~> 0.7.0', require: 'google/api_client'
Problem is, I've tried so many times for so long, I am confused as to what the problem could be.
Starting after setting up Omniauth, how do I pull Google Calendar information in my rails app?
Assuming user has authorized your application and you have the access token and refresh token stored and the access token has the scope to access the user' calendar.
First intialize the client and get a new access_token and specify the service you want to use and its version
client=Google::APIClient.new(:auto_refresh_token => true)
client.authorization.access_token = token
client.authorization.refresh_token = refresh_token
client.authorization.client_id = client_id
client.authorization.client_secret = client_secret
token = client.authorization.fetch_access_token["access_token"]
service = client.discovered_api('calendar' , 'v3')
use the client object to invoke any api method, here I'm calling 'events.list' to get user events from the calendar
response = client.execute(api_method: service.events.list, parameters: { 'calendarId' => 'primary' }, headers: { 'Content-Type' => 'application/json' })
I've been looking so hard for the correct solution to this using google-api-client version
0.9
and I was very grateful to run into Sophie's blog post with Josh' comment. Here's the quick recap of how I got it to work with their input:It might also help to read the migration to 0.9 manual to see how things have changed with this advent of this new gem version.
I finally got it to work.
I added :google_access_token, :google_refresh_token, and :google_expires_at to my user model. and used:
in my omniauth callbacks controller to store it and this to access it.
Could sby tell me, what's the difference between Sebhastien's and my solutions? (My code is not ready yet and don't know yet if I can get the new access_token with my solution.)
1.I just don't get it when and how I can get the new access token and if I need to deal with refresh_token and expires_at attributes.
2.How the
(:auto_refresh_token => true)
andfetch_access_token!
exactly works?