Creating a simple Google calendar event in PHP

I have time trying to get a very simple event added to the calendar using the Google Calendar API, and I would be very pleased if anyone could point out my (possibly obvious) problem. I am using the code I found here . I put the code in the "google-api-php-client / examples.calendar" directory, where you can find a simple example.

<?php require_once '../../src/Google_Client.php'; require_once '../../src/contrib/Google_CalendarService.php'; session_start(); $client = new Google_Client(); $client->setApplicationName("Google Calendar PHP Starter Application"); $client->setClientId(''); $client->setClientSecret(''); $client->setRedirectUri('worked.html'); //I made a file called "worked.html" in the same directory that just says "it worked!" $client->setDeveloperKey('SecretLongDeveloperKey'); $cal = new Google_CalendarService($client); if (isset($_GET['logout'])) { unset($_SESSION['token']); } if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['token'] = $client->getAccessToken(); header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); } if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); } $authUrl = $client->createAuthUrl(); if (!$client->getAccessToken()){ $event = new Google_Event(); $event->setSummary('Halloween'); $event->setLocation('The Neighbourhood'); $start = new Google_EventDateTime(); $start->setDateTime('2012-10-31T10:00:00.000-05:00'); $event->setStart($start); $end = new Google_EventDateTime(); $end->setDateTime('2012-10-31T10:25:00.000-05:00'); $event->setEnd($end); $createdEvent = $cal->events->insert(' secretLongCalendarId@group.calendar.google.com ', $event); } echo $createdEvent->getId(); ?> 

When I access this script, I get 404 error. I tried to go through the code and comment out the lines, trying to find the culprit - this seems to be the second by last line that actually inserts the event.

Any tips? I would really appreciate some pointers, because, in my opinion, I can't get even the simplest examples to work with.

+4
source share
2 answers

Your code is almost working.

However, you are redirected to "work.html". Therefore, your event is not raised after authentication redirection. Also, setRedirectUri should correspond to what you entered in the Google API plus the console (see "URI Redirection") And it should be THIS because this file introduces an event after the redirect, (You do not need "work.html")

So your simple.php should look like this (ALSO change the "redirect URI" in Google Api to http://localhost/simple.php , you need to specify the domain, but you can use localhost, in setRedirectUri you can specify the same thing)

 <?php error_reporting(E_ALL); require_once 'google-api-php-client/src/Google_Client.php'; require_once 'google-api-php-client/src/contrib/Google_CalendarService.php'; session_start(); if ((isset($_SESSION)) && (!empty($_SESSION))) { echo "There are cookies<br>"; echo "<pre>"; print_r($_SESSION); echo "</pre>"; } $client = new Google_Client(); $client->setApplicationName("Google Calendar PHP Starter Application"); $client->setClientId('###'); $client->setClientSecret('###'); $client->setRedirectUri('http://###/index.php'); $client->setDeveloperKey('###'); $cal = new Google_CalendarService($client); if (isset($_GET['logout'])) { echo "<br><br><font size=+2>Logging out</font>"; unset($_SESSION['token']); } if (isset($_GET['code'])) { echo "<br>I got a code from Google = ".$_GET['code']; // You won't see this if redirected later $client->authenticate($_GET['code']); $_SESSION['token'] = $client->getAccessToken(); header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); echo "<br>I got the token = ".$_SESSION['token']; // <-- not needed to get here unless location uncommented } if (isset($_SESSION['token'])) { echo "<br>Getting access"; $client->setAccessToken($_SESSION['token']); } if ($client->getAccessToken()){ echo "<hr><font size=+1>I have access to your calendar</font>"; $event = new Google_Event(); $event->setSummary('Halloween'); $event->setLocation('The Neighbourhood'); $start = new Google_EventDateTime(); $start->setDateTime('2013-9-29T10:00:00.000-05:00'); $event->setStart($start); $end = new Google_EventDateTime(); $end->setDateTime('2013-9-29T10:25:00.000-05:00'); $event->setEnd($end); $createdEvent = $cal->events->insert('###', $event); echo "<br><font size=+1>Event created</font>"; echo "<hr><br><font size=+1>Already connected</font> (No need to login)"; } else { $authUrl = $client->createAuthUrl(); print "<hr><br><font size=+2><a href='$authUrl'>Connect Me!</a></font>"; } $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; echo "<br><br><font size=+2><a href=$url?logout>Logout</a></font>"; ?> 

Also, as @BigMacAttack has already said, you only need $authURL = $client->createAuthURL(); once only if getAccessToken failed.

Happy Halloween -)

Change I cleaned up a lot of the code using work links for logging in and out, and log messages.

+3
source

The last part of your code does not use the correct logic. The essence of the problem is if (!$client->getAccessToken()) . This operator in the context of your sample code roughly translates to: If you were unable to get the access token, create an event. Obviously this is not what you want.;)

Instead, try the following:

 if ($client->getAccessToken()){ //succeeded in getting an access token, so create and insert the event } else { $authUrl = $client->createAuthUrl(); //failed to get an access token, so display $authurl as a link to open the auth window } 
0
source

Source: https://habr.com/ru/post/1501229/


All Articles