0

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()?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Peter Westerlund
  • 741
  • 1
  • 10
  • 36

2 Answers2

0

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.

  1. Try and catch the error,
  2. I suspect the issue is with your delegation. Did you follow this page domain wide deligation
  3. 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('mail@YourDomain.com');
$client->setAccessType('offline');

$service = new Google\Service\Calendar($client);
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

As mentioned, the main obstacle in the way to figure out what's wrong had to do with the fact that I didn't get any error messages.

So to be able to get error messages I started to try connect with cURL instead to get error codes. It gave me a 403 code and from that I understood that it was something with the authentication rather than a syntax error or something else.

So I started too look through my Google Cloud set-up again while also Google around on what could cause this error code. And I came over one comment in a thread here on this forum that helped me proceed:

Nevermind! I figured it out! I had forgotten to activate the API within the Google Development Portal. They should really put a big poster that reminds you when you make a new project.

So, I had added the Calendar API but missed to actually Enable it too which you have to do manually by clicking an "Enable" button after you added the API.

So that made the code to work. Then I ran in to more problems but those I could solve by myself...

But yeah, make sure to enable/activate the Calendar API in Google Cloud, Library section.

Peter Westerlund
  • 741
  • 1
  • 10
  • 36