0

I'm not very good with PHP, and I am trying to create a webpage where I will navigate to the page and it will show a list of events from my private google calendar. This used to work years ago, but Google has changed the API so my old solution is broken. I saw this thread: How do I connect to the Google Calendar API without the oAuth authentication? and I tried it, but it looks like that method also doesn't work anymore. (When I say "doesn't work" I mean that I currently have the latest google-api-php-client-2.0.3 installed at public_html/includes/google-api-php-client-2.0.3/. I found the client.php referenced in the above link, but the other files are not in the new API.

Basically, I am trying to figure out what the PHP would look like to achieve what you can do on this page when you put in your calendar ID: https://developers.google.com/google-apps/calendar/v3/reference/events/list#request

So I think this is a two step process: Authenticate and then print the output. The trouble I'm having is in authenticating.

I have tried this:

<?php
session_start();
require_once "/includes/google-api-php-client-2.0.3/src/Google/client.php";
require_once "/includes/google-api-php-client-2.0.3/src/contrib/Google_CalendarService.php";

const CLIENT_ID = '...';
const SERVICE_ACCOUNT_NAME = 'my service account name';

// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '...';

$client = new Google_Client();
$client->setApplicationName("...");


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

// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/calendar', "https://www.googleapis.com/auth/calendar.readonly"),
    $key)
);

$client->setClientId(CLIENT_ID);
$service = new Google_CalendarService($client);

//Save token in session
if ($client->getAccessToken()) {
  $_SESSION['token'] = $client->getAccessToken();
}

//And now you can use the code in their PHP examples, like: $service->events->listEvents(...)
?>

But I get error:

Warning: require_once(/includes/google-api-php-client-2.0.3/src/Google_Client.php): failed to open stream: No such file 

Because the API seems to have changed.

I found this link: https://developers.google.com/identity/protocols/OAuth2ServiceAccount

I have created a service account, and downloaded a private key but I don't know what to do with it :-/

I tried this: https://developers.google.com/api-client-library/php/auth/web-app

But when I navigate to the index.php I get this error:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'file does not exist' in /home/xxx/public_html/includes/google-api-php-client-2.0.3/src/Google/Client.php:841 Stack trace: #0 /home/xxx/public_html/testing_oauth.php(7): Google_Client->setAuthConfig('client_secrets....') #1 {main} thrown in /home/xxx/public_html/includes/google-api-php-client-2.0.3/src/Google/Client.php on line 841

I tried hard coding this into the index.php example (line 103-107):

  // https://developers.google.com/console
  'client_id' => 'My service accounts project name',
  'client_secret' => 'url link to my client_secret.json file',
  'redirect_uri' => null,
  'state' => null,

But I get the same error...

Community
  • 1
  • 1
Joel
  • 2,691
  • 7
  • 40
  • 72
  • 1
    Asking for tutorials/guides is EXPLICITLY off-topic. – Marc B Nov 01 '16 at 15:47
  • lol-what happened to this place? Shall I list all the code that I have tried that hasn't worked yet? – Joel Nov 01 '16 at 15:56
  • 1
    Might be a good point to start: [Google Calendar API - PHP Quickstart](https://developers.google.com/google-apps/calendar/quickstart/php) – simon Nov 01 '16 at 16:05
  • @simon-yes I started there. That is for an app-not a server to server situation. I need to use oAuth: https://developers.google.com/api-client-library/php/auth/web-app I tried those examples but am getting an error-I will post those errors in an edit. – Joel Nov 01 '16 at 16:19
  • This may have something to do with your "require_once" configuration which is similar to [this thread](http://stackoverflow.com/questions/24293493/required-once-issue-in-google-api-client-for-php). – ReyAnthonyRenacia Nov 02 '16 at 08:46

0 Answers0