Google日历api CalendarList列表返回空列表

时间:2016-05-05 11:38:23

标签: php google-calendar-api

已下载脚本here并进行设置。但是当我尝试获取日历时,它会返回空列表。我想得到所有的日历列表 与帐户共享。请注意,我的Gmail帐户共享了4个日历。我也遵循这个instruction

**Code**

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


$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$service = new Google_Service_Calendar($client);
echo "<br>=======================<br>";    
var_dump($service->calendarList->listCalendarList()); 
echo "<br>=======================<br>";    
?>

输出

=======================

object(Google_Service_Calendar_CalendarList)[23]   protected 'collection_key' => string 'items' (length=5)   protected 'internal_gapi_mappings' => 
    array (size=0)
      empty   public 'etag' => string '"1462447526879000"' (length=18)   protected 'itemsType' => string 'Google_Service_Calendar_CalendarListEntry' (length=41)   protected 'itemsDataType' => string 'array' (length=5)   public 'kind' => string 'calendar#calendarList' (length=21)   public 'nextPageToken' => null   public 'nextSyncToken' => string 'CJi-3srpwswCEjVhYmNkZWZAc2tpbGxmdWwtY29zaW5lLTEyNDYwNi5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbQ==' (length=88)   protected 'modelData' => 
    array (size=1)
      'items' => 
        array (size=0)
          empty   protected 'processed' => 
    array (size=0)
      empty
=======================

1 个答案:

答案 0 :(得分:0)

当文件处于json格式下面时,读取密钥有一些变化是正确的代码

$key = file_get_contents($key_file_location);
$data = json_decode($key); 

    $scopes = array("https://www.googleapis.com/auth/calendar.readonly","https://www.googleapis.com/auth/calendar");
    $cred = new Google_Auth_AssertionCredentials(
        $service_account_name,
        $scopes,
        $data->private_key
    );
$client->setAssertionCredentials($cred);


try {
  $client->getAuth()->refreshTokenWithAssertion($cred);
} catch (Exception $e) {
    $e->getMessage();
}
$calendarList  = $service->calendarList->listCalendarList();
var_dump($calendarList);
相关问题