Reputation: 383
I have seen many posts on this subject, but none have been answered, and most are closed because of their vague nature. Hopefully I can write something a little more explanatory.
I've been writing a program which loads data from my clients Google Calendar. The OAuth2 'access_token' for this calendar is kept in the database and passed whenever I need to access it. As such, it always accesses the same Calendar owned by my client.
My client then updates this Google Calendar, and by way of the Google Calendar API and a bit of AJAX, I draw in his free/busy data and update a flat HTML page for the user.
This way it acts like a booking system. The user can manipulate the HTML page and the days they select are then updated on my clients Google Calendar via the Google Calendar API.
Here's the problem.
For some reason I don't get a 'refresh_token' and as such after an hour, my 'access_token' is obsolete and I can't access the Calendar data.
These are the values I am sending for my 'code' (don't worry about the formatting, this is just to show you):
'redirect_uri' : 'http://local.test.com',
'client_id' : 'blahblah.apps.googleusercontent.com',
'scope' : 'https://www.googleapis.com/auth/calendar',
'access_type' : 'offline',
'response_type' : 'code'
Once I have retrieved my 'code', I then make the second request for my 'access_token':
'code': $code,
'grant_type': 'authorization_code' ,
'redirect_uri' : 'http://local.test.com',
'client_id' : 'blahblah.apps.googleusercontent.com',
'client_secret' : 'secret_code'
So I get back everything I need, just no 'refresh_token'.
Can anyone point out where I'm going wrong here? Can anyone suggest perhaps a better solution based on my case, or even if i'm trying the impossible?
Any help would be greatly appreciated
I'm using Google Calendar API v3
Upvotes: 2
Views: 2230
Reputation: 383
For anyone who stumbles across this post.
The above steps to get your 'access_token' and 'refresh_token' are correct. However Google has changed the way it issues OAuth2 tokens.
Your first ever request will grant you a 'refresh_token', after that you will not receive another one. So if you lose it, you will have to set up completely new client secrets etc in Google's API Console.
Upvotes: 2