Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch Google Calendar Events on PHP server after authenticating on iOS app

Okay so here's what I want to do...

  1. user authorized the app on iPhone with read/write access to Google calendar
  2. I get an auth token of some kind from Google
  3. Pass on the token to my PHP server and save it in database
  4. Use the token to regularly check on Google events and store them in server's database
  5. Send the Google events data as json to the app

I want to implement the fetching of Google events on server, so that I can build additional functionality around them like sending remote push notifications.

I am stuck at the part of getting auth token and saving it on server and using it to fetch events from Google's calendar api, having not able to find the resources for same. Can somebody throw some light on this.


UPDATE

I have been able to successfully implement the scenario till step 3.

I get the auth token and refresh token from Google and save it in database. Now using the Google API php client, I am trying to connect to Google's calendar API using the access token that I got earlier. I am using the following code...

require_once(APPPATH . "libraries/Google/Google_Client.php");
require_once(APPPATH . "libraries/Google/contrib/Google_CalendarService.php");

$client = new Google_Client();
$client->setAccessToken($google_access_token);

$calendar = new Google_CalendarService($client);
$calendarList = $calendar->calendarList->listCalendarList();
echo print_r($calendarList, true);

But now I get this error...

PHP Fatal error: Uncaught exception 'Google_AuthException' with message 'Could not json decode the token' in /myserver/application/libraries/Google/auth/Google_OAuth2.php:162

Stack trace: /myserver/application/libraries/Google/Google_Client.php(170): Google_OAuth2->setAccessToken('a_token...')

I understand that I am directly trying to set the access token in Google client api without specifying any redirect url or other params normally used when the user authorizes the access to Google calendars on server itself. Is this supposed to work like I am trying to?


UPDATE 2

Upon some further digging, I found out that directly setting the access token using setAccessToken does not work as Google client for API expects a JSON encoded string in the setAccessToken method. After some tweaks I changed my code to following....

require_once(APPPATH . "libraries/Google/Google_Client.php");
require_once(APPPATH . "libraries/Google/contrib/Google_CalendarService.php");

$client = new Google_Client();
$client->setAccessType('offline');
$client->refreshToken($google_refresh_token);
$newToken = $client->getAccessToken();
$client->setAccessToken($newToken);

$calendar = new Google_CalendarService($client);
$calendarList = $calendar->calendarList->listCalendarList();
echo print_r($calendarList, true);

Now the error that I am getting is that of an invalid_request.

Error refreshing the OAuth2 token, message: '{"error" : "invalid_request"}'

like image 913
vikmalhotra Avatar asked Oct 22 '22 02:10

vikmalhotra


1 Answers

Finally I am able to answer my own question. Hope this helps someone else.

Step 1 & 2. I got the Google OAuth protocol working in my iOS App following the instructions in this excellent tutorial on mobiletuts.

Step 3. I saved the access token and refresh token in database on my web server.

Step 4. Using the refresh token I got from iOS App I connected to the Google calendar API with this code...

require_once(APPPATH . "libraries/Google/Google_Client.php");
require_once(APPPATH . "libraries/Google/contrib/Google_CalendarService.php");

/* setup google client object */
$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);

/* refresh access token */
$client->refreshToken($google_refresh_token);
$newToken = $client->getAccessToken();
$client->setAccessToken($newToken);

/* get google calendars list */
$calendar = new Google_CalendarService($client);
$calendarList = $calendar->calendarList->listCalendarList();
echo print_r($calendarList, true);
like image 112
vikmalhotra Avatar answered Oct 27 '22 11:10

vikmalhotra