Cellydy
Cellydy

Reputation: 1339

Google API Offline Access To User Data

I'm working on an application that requires permission to access users Google Calendars. I'm using the PHP client and I'm able to get offline access to these calendars. My issue is as follows: once I have permission, how do I access the calendars for a specific users at a later date? How do I create a client object for a specific user (that has already given me permission to access their calendars) when I need it?

I've managed to extract a client ID using the Google_Service_Oauth2::USERINFO_EMAIL scope but how could I use it to get that users calendars at a later date? Is that even how I should be going about it or am I heading in the wrong direction?

Upvotes: 1

Views: 646

Answers (1)

Morfinismo
Morfinismo

Reputation: 5253

There are several approaches you can take to achieve this. In your case, since you only want to access user calendar, then the scopes you should include in your requests are the one specified here. Your client configuration should have $client->setAccessType("offline"); and $client->setApprovalPrompt("force");

After allowing access, you will be returned an access code that you can exchange for an access token. The access token returned is the one you need to save in a database. Later on, if the user needs to use the calendar service, you simply use the access token you already saved.

If you need code specifics, then please post code specifics since we are not able to fully understand what you are trying to achieve without looking at your implementation.

Take for example the following code snippet.

/*
 * @$accessToken - json encoded array (access token saved to database)
*/

$client = new Google_Client();
$client->setAuthConfig("client_secret.json");
$client->addScope("https://www.googleapis.com/auth/calendar");

$_SESSION["access_token"] = json_decode($accessToken, true);

$client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_Calendar($client);

//REST OF THE PROCESS HERE

Upvotes: 1

Related Questions