2014-04-12 61 views
1

我試圖添加和發送電子郵件事件使用谷歌日曆Api 3,但谷歌Api返回403禁止和我的日曆設置爲公開。 我的代碼是:谷歌日曆API創建事件返回403禁止和日曆公開 - PHP

ini_set('display_errors', 1); 
require 'Google/Client.php'; 
require 'Google/Service/Calendar.php'; 

session_start(); 

const CLIENT_ID = '**************.apps.googleusercontent.com'; 
const SERVICE_ACCOUNT_NAME = '**************@developer.gserviceaccount.com'; 
const KEY_FILE = '**************-privatekey.p12'; 

$client = new Google_Client(); 
$client->setApplicationName("Calendar App"); 
//$client->setUseObjects(true); //IF USING SERVICE ACCOUNT (YES) 

if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
} 

$key = file_get_contents(KEY_FILE); 
$client->setClientId(CLIENT_ID); 
$client->setAssertionCredentials(new Google_Auth_AssertionCredentials(
SERVICE_ACCOUNT_NAME, 'https://www.google.com/calendar/feeds/**************@group.calendar.google.com/private/full/', 
$key) 
); 

$client->setClientId(CLIENT_ID); 

$cal = new Google_Service_Calendar($client); 
$event = new Google_Service_Calendar_Event(); 
$event->setSummary('App Sum'); 
$event->setLocation('App Location'); 

$start = new Google_Service_Calendar_EventDateTime(); 
$start->setDateTime('2014-05-19T10:30:00.000-05:00'); 
$event->setStart($start); 

$end = new Google_Service_Calendar_EventDateTime(); 
$end->setDateTime('2014-05-20T11:30:00.000-05:00'); 
$event->setEnd($end); 

$attendeel = new Google_Service_Calendar_EventAttendee(); 
$attendeel->setEmail('**************@gmail.com'); 
$attendees = array($attendeel); 
$event->attendees = $attendees; 

$cal->events->insert('**************@group.calendar.google.com', $event); 

http://amazewebs.yitweb.com/#scriptv4取碼,有一個類似的代碼Edit Google calendar events from Google service account: 403當開發商說,這是不是代碼,如果沒有日曆設置

難道他們知道,如果有一些其他的配置還是代碼真的是問題?

回答

0

我一直在玩這個東西有點太。我得到了列表事件的工作,但後來切換到添加事件,我得到了這個401.我發現你必須與**************@developer.gserviceaccount共享日曆。 com電子郵件。您可以通過轉到日曆>我的日曆>(姓名)>共享此日曆,然後在與特定人共享下添加該gserviceaccount電子郵件並設置適當的權限。

非常感謝這個post和這個post

1

如果您有權訪問域管理員帳戶,則可以將日曆更新作爲該帳戶發佈,而無需以管理員身份登錄。你可以填寫$client的所有參數:

setAssertionCredentials(new Google_Auth_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,   
    'https://www.google.com/calendar/feeds/**************@group.calendar.google.com/private/full/', 
    $key, 
    'notasecret', 
    'http://oauth.net/grant_type/jwt/1.0/bearer', 
    'your admin account email');