2015-12-03 40 views
0

感谢您的时间。我已经找到了关于如何为公共日历执行此操作的文档,但我需要它来提供不会与世界共享的日历。需要从日历API中读取而不公开日历

这是我的代码:

include(__DIR__ . '/composer/vendor/google/apiclient/autoload.php'); 

$client_email = '[email protected]'; 
$private_key = file_get_contents('/home/serverSecret.json'); 
$scopes = array(Google_Service_Drive::DRIVE_METADATA_READONLY); 
$credentials = new Google_Auth_AssertionCredentials($client_email, $scopes, $private_key); 

$client = new Google_Client(); 
$client->setAssertionCredentials($credentials); 
if ($client->getAuth()->isAccessTokenExpired()) 
{ 
    $client->getAuth()->refreshTokenWithAssertion(); 
} 

$client->setApplicationName("A Calendar"); 
$cal = new Google_Service_Calendar($client); 
$calendarId = '[email protected]'; 
$params = array('singleEvents' => true, 'orderBy' => 'startTime', 'timeMin' => date(DateTime::ATOM), 'maxResults' => 7); 
$events = $cal->events->listEvents($calendarId, $params); 
$calTimeZone = $events->timeZone; 
date_default_timezone_set($calTimeZone); 

foreach ($events->getItems() as $event) 
{ 
    // Get the timings 
    $eventDateStr = $event->start->dateTime; 
    if(empty($eventDateStr)) { $eventDateStr = $event->start->date; } // Handle all-day events 
    $temp_timezone = $event->start->timeZone; 

// Timezone override if applicable 
if (!empty($temp_timezone)) { $timezone = new DateTimeZone($temp_timezone); } 
else { $timezone = new DateTimeZone($calTimeZone); } 

// Set up the timings 
$eventdate = new DateTime($eventDateStr,$timezone); 
$link = $event->htmlLink; 
$TZlink = $link . "&ctz=" . $calTimeZone; 
$newmonth = $eventdate->format("M"); 
$newday = $eventdate->format("j"); 
?> 
<div class="event-container"> 
    <div class="eventDate"> 
    <span class="month"><?=$newmonth?></span> 
    <br /> 
    <span class="day"><?=$newday?></span> 
    <span class="dayTrail"></span> 
    </div> 

    <div class="eventBody"> 
     <a href="<?=$TZlink?>"><?=$event->summary?></a> 
    </div> 
</div> 
<?php 
} 
?> 

上述工作,当我使用客户端密钥,但不是当日历是由私人公共日历使用它。我需要知道如何对私人日历进行身份验证。

回答

0

您需要的OAuth 2.0服务来访问私人数据:您可能需要创建一个服务帐户:

通常,应用程序使用服务帐户当应用程序 使用谷歌的API一起工作的拥有数据而不是用户的数据。 例如,使用Google Cloud Datastore获取数据 持久性的应用程序将使用服务帐户来验证其对Google Cloud Datastore API的 的调用。

​​