I'm trying to add calendar events to my Google calendar from a php script. Note that I can do this successfully directly from a Google test page so I think that my calendar is set up correctly. However, when I attempt do this from a php script, I'm not able to get the OAuth2 token.
The following script, mostly taken from a Google example, runs without error, but it always winds up with a "Connect Me!" link. When I click on this link, it takes me to a validation screen that warns me that this app would like to manage my calendars. I click 'Accept' and a then see my calendar. A little inconvenient, but if this gets the OAuth token, fine.
I then go back and run the script again, hoping that the access token will now be available, but I get exactly the same result. No matter how many times I run this script, I never get the token, which I need in order to expand the script to add events to the calendar.
I've been at this for days, and I'm just not getting anywhere. Can anyone help? Thanks.
<?php
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
require_once '../../src/auth/Google_OAuth2.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Add Google Calendar Entries");
// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('843319906820-1s1d737e77o71a3vaskf434k3ape1fk5.apps.googleusercontent.com');
$client->setClientSecret('AP5imn3e0TEWNyLTCNm8YJj6');
$client->setRedirectUri('https://www.google.com/calendar/');
$client->setDeveloperKey('AIzaSyDIw3ks7AmrIrRxjO9y2gWBhQDYHFWd-uc');
$client->setUseObjects(true);
$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']);
}
if ($client->getAccessToken()) {
$calList = $cal->calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>
EDIT: Attempting to implement the suggestion from Vinicius Pinto. I've changed my redirect to point directly back to this script. Now I get this error:
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/calendar/v3/users/me/calendarList?key=AIzaSyDIw3ks7AmrIrRxjO9y2gWBhQDYHFWd-uc: (403) Access Not Configured. Please use Google Developers Console to activate the API for your project.' in D:\Hosting\11347607\html\scripts\google-api-php-client\src\io\Google_REST.php:66 Stack trace: #0 D:\Hosting\11347607\html\scripts\google-api-php-client\src\io\Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 D:\Hosting\11347607\html\scripts\google-api-php-client\src\service\Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 D:\Hosting\11347607\html\scripts\google-api-php-client\src\contrib\Google_CalendarService.php(205): Google_ServiceResource->__call('list', Array) #3 D:\Hosting\11347607\html\scripts\google-api-php-client\examples\calendar\simple.php(43): Google_CalendarListServiceResource->listCalen in D:\Hosting\11347607\html\scripts\google-api-php-client\src\io\Google_REST.php on line 66
It would appear that I don't have the correct APIs activated in the developer console. I have the Calendar API
and the Google Cloud Storage JSON API
activated and nothing else. I don't see any other APIs that would be relevant, and I don't think that this message is telling me which APIs it wants.
EDIT 2: Looks like I was using the wrong developer key. I've changed it to the key for server applications. Now my error is this:
Fatal error: Uncaught exception 'Google_AuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_client" }'' in D:\Hosting\11347607\html\scripts\google-api-php-client\src\auth\Google_OAuth2.php:288 Stack trace: #0 D:\Hosting\11347607\html\scripts\google-api-php-client\src\auth\Google_OAuth2.php(248): Google_OAuth2->refreshTokenRequest(Array) #1 D:\Hosting\11347607\html\scripts\google-api-php-client\src\auth\Google_OAuth2.php(225): Google_OAuth2->refreshToken('1/6ZdJyZALqcbwW...') #2 D:\Hosting\11347607\html\scripts\google-api-php-client\src\service\Google_ServiceResource.php(167): Google_OAuth2->sign(Object(Google_HttpRequest)) #3 D:\Hosting\11347607\html\scripts\google-api-php-client\src\contrib\Google_CalendarService.php(205): Google_ServiceResource->__call('list', Array) #4 D:\Hosting\11347607\html\scripts\google-api-php-client\examples\calendar\simple.php(47): Google_CalendarListServiceResource->listCalendarList() in D:\Hosting\11347607\html\scripts\google-api-php-client\src\auth\Google_OAuth2.php on line 288