Search code examples
phpgoogle-apioauth-2.0laravel

How to integrate Google calendar with API in Laravel?


I want to build a Google calendar interface into my web app and use the calendar as storage for events as this also feeds other services. I know I need to get an authorization token and then a refresh token for the app to use the API. I've set up the app on Google and sourced PHP code to help get the token.

However I'm really struggling to understand what I'm doing and what the code is doing and haven't found an idiots guide on this topic as a step by step... I've probably made basic mistakes but not sure what they are.

I found this post: Google Calendar API v3 hardcoded credentials which I've tried to follow.

I ran the first script:

<?php
$scope         =   'https://www.google.com/calendar/feeds/';
$client_id      =   'my id';
$redirect_uri   =   'my redirect url';

$params = array(
                'response_type' =>   'code',
                'client_id'     =>   $client_id,
                'redirect_uri'  =>   $redirect_uri,
                'scope'         =>   $scope
                );
$url = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($params);        
echo $url."\n";

Questions: I copied the redirect URI from the Google API. This includes some code and also on another line http://localhost. Do I copy both lines or just one. Should this be customized for my app in anyway. What does this parameter do? -

I'm running on a local machine at the moment with a server name of downsadmin.loc. My intention is to run eventually on a live server once built.

I ran the script with the uri provided by Google API and it came up with an error:

Invalid parameter value for redirect_uri: Missing authority: urn:ietf:wg:oauth:2.0:oob http://localhost

So I assume my redirect URI is wrong?

I've just downloaded the Google PHP library. I ran the example here:

$client->setClientId('my id');
$client->setClientSecret('my key_');
$client->setRedirectUri('uri
');
$client->setDeveloperKey('my developer key');
$plus = new Google_PlusService($client);

if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
$activities = $plus->activities->listActivities('me', 'public');
print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>';

// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a href='$authUrl'>Connect Me!</a>";
}

Again an error:

invalid parameter value for redirect_uri: Missing authority: urn:ietf:wg:oauth:2.0:oob http://downsadmin.loc/google/test_googleplus.php

Any help and pointers appreciated - especially a really basic link to a step by step of what to do and why - and then next steps how to then start using the API.


Solution

  • Ok

    More research in desperation and I found this tutorial on oauth 2. I've followed this in step 2 and managed to get my refresh token!

    http://cornempire.net/2012/01/08/part-2-oauth2-and-configuring-your-application-with-google/

    I did initially have a blank token returned and needed to add the following to the script as per a comment by user 'Bac' in the post:

    To fix the empty $cRefreshToken issue you need to enable SSL by adding

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    

    right before curl_exec($ch);

    So now on to working with the API