Keeping Google API sessions persistent with refresh tokens
February 13, 2012
I’ve been working with Google’s Tasks API and was looking to have a way a user would only need to authenticate their account once. Google does not explain very well on how to achieve this.
With php you will need to get the api setup HERE. You then need to then setup up an authentication stream:
require_once 'src/apiClient.php';
require_once 'src/contrib/apiTasksService.php';
$client = new apiClient();
$tasksService = new apiTasksService($client);
On the callback of the user allowing your app to access their tasks, you will get an access token and more importantly here a refresh token.
$auth = $client->authenticate();
$token = $client->getAccessToken();
$refresh_token = $token['refresh_token'];
Save the refresh token to somewhere you are able to access it again whenever you’d like. After that we can use that token to get the users tasks at any moment.
$client->refresh_token($refresh_token);