Google Calendar API with PHP – Service Account

2019-08-09 07:56发布

I've been trying for several days now to get a Google Calendar API up and running with absolutely no luck. I'm using PHP and a service account for authentication. I have followed this tutorial (http://www.daimto.com/google-calendar-api-with-php-service-account/) and the code below is from that tutorial.

I have setup my developers console to allow the calendar API, and I have added the developers email address to the calendar share section.

When I run this code, I just get a blank page - no error, no text of any kind, just a blank white page. I'm completely lost - any help would be much appreciated. Cheers

<?php

require 'vendor/autoload.php';
session_start();        

$client_id = '123456789-qwertyuiop.apps.googleusercontent.com';
$Email_address = '123456789-qwertyuiop@developer.gserviceaccount.com';   
$key_file_location = 'path_to_key/key.p12';     

$client = new Google_Client();      
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);    

// separate additional scopes with a comma   
$scopes ="https://www.googleapis.com/auth/calendar.readonly";   
$cred = new Google_Auth_AssertionCredentials(    
    $Email_address,      
    array($scopes),     
    $key         
);      
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {        
    $client->getAuth()->refreshTokenWithAssertion($cred);       
}       
$service = new Google_Service_Calendar($client);    

?>

<html><body>

<?php
$calendarList  = $service->calendarList->listCalendarList();

while(true) {
    foreach ($calendarList->getItems() as $calendarListEntry) {

        echo $calendarListEntry->getSummary()."<br>\n";

        // get events 
        $events = $service->events->listEvents($calendarListEntry->id);

        foreach ($events->getItems() as $event) {
            echo "-----".$event->getSummary()."<br>";
        }
    }
    $pageToken = $calendarList->getNextPageToken();
    if ($pageToken) {
        $optParams = array('pageToken' => $pageToken);
        $calendarList = $service->calendarList->listCalendarList($optParams);
    } else {
        break;
    }
}

?>

1条回答
家丑人穷心不美
2楼-- · 2019-08-09 08:28

When I run this code, I just get a blank page - no error, no text of any kind, just a blank white page. I'm completely lost - any help would be much appreciated. Cheers

On this note, I was running into a similar frustration and discovered that my script automatically created an error_log in a separate file on the server. Just open it in your code editor and you can see detailed responses. Not sure if this will help you at all, but it helped me a ton.

查看更多
登录 后发表回答