So I'm trying to set up an API connection to Google Calendar to be able to create and update events and so on. First, I'm just trying to get the API connection to work.
So I have installed the Google API PHP Client library to my project folder on my website. Then created a Service Account at the IAM & Admin section at the Google Cloud console.
I have created a key for the Service Account and downloaded and uploaded the JSON-file to the project folder on my web server. The JSON file is in the same folder as the Google API connection code where I'm trying to make this working. My code looks like this:
$client = new Google\Client();
$credentials_file = "myproject-123456789.json";
$auth_data = json_decode(file_get_contents($credentials_file), TRUE);
$client->setAuthConfig($auth_data);
$client->setApplicationName("myproject");
$client->setScopes(['https://www.googleapis.com/auth/calendar']);
$service = new Google\Service\Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
But the page crashes. It has something to do with the listCalendarList()
function above because removing that makes it not crashing. So what could it be?
I don't get any error messages anywhere and not even when removing that last line returns in any error message so I assume the authentication is working (?). But why can't I call listCalendarList()
?
CodePudding user response:
This is the first problematic line:
$client = new Google\Client();
I copied your code and got this fatal error:
Fatal error: Uncaught Error: Class 'Google\Client' not found in /home3/el/public_html/sandbox.php:3 Stack trace: #0 {main} thrown in /home3/el/public_html/sandbox.php on line 3
CodePudding user response:
I would start with trying to catch the error message. Without seeing the error message that is Being returned the only thing i see wrong is that you have not SetSubject
.
I would try debugging in this order.
- Try and catch the error,
- I suspect the issue is with your delegation. Did you follow this page domain wide deligation
- setSubject is the email address of the user on your domain that you want the service account to impersonate.
example
$client = new Google\Client();
$client->setAuthConfig('./secret.json');
$client->setApplicationName('app name');
$client->addScope(Google\Service\Calendar::CALENDAR);
$client->setSubject('[email protected]');
$client->setAccessType('offline');
$service = new Google\Service\Calendar($client);