Google Calendar API插入活动

时间:2015-04-15 18:29:35

标签: php google-calendar-api google-api-php-client

所以,我下载了这个API库https://github.com/google/google-api-php-client.git 并将其安装在服务器上,然后设置非常简单的脚本将事件添加到我的谷歌日历中,脚本的第一部分确保您的连接良好,第二部分尝试添加事件。所以,连接很好,但添加事件不是..它让我回来了

`Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events: (403) Insufficient Permission'` 

这是脚本,有人可以帮忙吗?

<?php
session_start();        
require '../google-api-php-client-master/src/Google/autoload.php';
require_once '../google-api-php-client-master/src/Google/Client.php';
require_once '../google-api-php-client-master/src/Google/Service/Calendar.php';     

$client_id = '574154490008-l55646vdpf62f7nrim9dodhtg6ssv4qv.apps.googleusercontent.com';
$Email_address = '574154490008-l55646vdpf62f7nrim9dodhtg6ssv4qv@developer.gserviceaccount.com';  
$key_file_location = 'someproject-9e09f9db466c.p12';        
$client = new Google_Client();      
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);

// seproate 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();
print_r($calendarList);
while(true) {
    foreach ($calendarList->getItems() as $calendarListEntry) {
        echo "<a href='Oauth2.php?type=event&id=".$calendarListEntry->id." '>".$calendarListEntry->getSummary()."</a><br>\n";
    }
    $pageToken = $calendarList->getNextPageToken();
    if ($pageToken) {
        $optParams = array('pageToken' => $pageToken);
        $calendarList = $service->calendarList->listCalendarList($optParams);
    } else {
        break;
    }
}    

    //--------------- trying to insert EVENT

    $event = new Google_Service_Calendar_Event();
    $event->setSummary('Appointment');
    $event->setLocation('Somewhere');
    $start = new Google_Service_Calendar_EventDateTime();
    $start->setDateTime('2015-04-16T10:00:00.000-07:00');
    $event->setStart($start);
    $end = new Google_Service_Calendar_EventDateTime();
    $end->setDateTime('2015-04-16T10:25:00.000-07:00');
    $event->setEnd($end);
    $attendee1 = new Google_Service_Calendar_EventAttendee();
    $attendee1->setEmail('some@gmail.com');
    // ...
    $attendees = array($attendee1
                   //, ...
                  );
    $event->attendees = $attendees;
    $createdEvent = $service->events->insert('primary', $event);
    echo $createdEvent->getId();



?>
</html>  

好的,所以我刚编辑了这一行$scopes ="https://www.googleapis.com/auth/calendar";我删除了“.readonly”所以现在我得到了eventId,就像我成功插入事件一样,但事件没有出现在日历中。

1 个答案:

答案 0 :(得分:-1)

您需要与服务帐户共享日历!

在现场电子邮件中发送您的电子邮件(服务帐户),这意味着:

914304219907-rt42vptejtgg9fhb9btthdb2gbuctonp@developer.gserviceaccount.com
相关问题