Jump to content

Connecting to Google API v3


dweb

Recommended Posts

Hey all

 

I'm working on a script which needs to connect to the Google Calendar API, but when I run the code I get

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' in /home/public_html/cal_test/google-api-php/src/Google/Auth/OAuth2.php:341 Stack trace: #0 /home/public_html/cal_test/google-api-php/src/Google/Auth/OAuth2.php(300): Google_Auth_OAuth2->refreshTokenRequest(Array) #1 /home/public_html/cal_test/google-api-php/src/Google/Auth/OAuth2.php(230): Google_Auth_OAuth2->refreshTokenWithAssertion() #2 /home/public_html/cal_test/google-api-php/src/Google/Service/Resource.php(171): Google_Auth_OAuth2->sign(Object(Google_Http_Request)) #3 /home/public_html/cal_test/google-api-php/src/Google/Service/Calendar.php(1133): Google_Service_Resource->call('list', Array, 'Google_Service_...') #4 /home/public_html/cal_test/index.php(34): Google_Service_Calendar_CalendarList_Resource->listCalendarList() #5 {main} thrown in /home/public_html/cal_test/google-api-php/src/Google/Auth/OAuth2.php on line 341

The code i'm using is

<?php

error_reporting(-1);
ini_set('display_errors', 'On');


require_once "google-api-php/src/Google/Client.php";
require_once "google-api-php/src/Google/Service/Calendar.php";


// Service Account info
$client_id = 'CLIENT_ID';
$service_account_name = 'EMAIL';
$key_file_location = 'FILE_LOC';

// Calendar id
$calName = 'CAL_ID';


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

$service = new Google_Service_Calendar($client);

$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
 $service_account_name,
 array('https://www.googleapis.com/auth/calendar.readonly'),
 $key
);

$client->setAssertionCredentials($cred);

$cals = $service->calendarList->listCalendarList();
//print_r($cals);
//exit;

$events = $service->events->listEvents($calName);

foreach ($events->getItems() as $event) {
 echo $event->getSummary();
}  
  ?>

i've spent serveral hours trying to locate a fix on Google, but so far i'm stuggling

 

Can anyone help me?

 

Thanks

Edited by dweb
Link to comment
Share on other sites

Well it's obviously authentication related, given the mention of OAuth2. Plus the exception is called "Google_Auth_Exception". Hate to say it, but are you 100% the username and password are correct?

Edited by Adam
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.