将事件添加到Google日历

时间:2012-04-10 06:13:04

标签: google-calendar-api

我们正在使用API​​ Google开发应用程序。在这个过程中,我们遇到了一些困难。

我们使用了此页面上的php-sdk“code.google.com/p/google-api-php-client/”我们使用了Google日历服务。我们按照此处的文档:“developers.google.com/google-apps/calendar/v3/reference/”部分日历和活动。

来源数据: - 此处的Google日历服务允许访问“code.google.com/apis/console/” - 需要用户授权(基于此处的文档:“developers.google.com/google-apps /日历/ V3 /参考/活动/插入)“

任务:将事件添加到日历。 操作:我们将帖子请求发送到https://www.googleapis.com/calendar/v3/calendars/ {calendarId} / events?calendarId = {calendarId}& alt = json& key = {API Key}

请求正文:

{

"\u0000*\u0000__creatorType":"EventCreator",

"\u0000*\u0000__creatorDataType":"",

"\u0000*\u0000__organizerType":"EventOrganizer",

"\u0000*\u0000__organizerDataType":"",

"\u0000*\u0000__attendeesType":"EventAttendee",

"\u0000*\u0000__attendeesDataType":"array",

"\u0000*\u0000__startType":"EventDateTime",

"\u0000*\u0000__startDataType":"",

"start":{

"date":"",

"timeZone":"Europe\/Moscow",

"dateTime":"2012-0408T12:00:00+04:00"

},

"location":"sdasdwqwqesaddsa",

"\u0000*\u0000__originalStartTimeType":"EventDateTime",

"\u0000*\u0000__originalStartTimeDataType":"",

"\u0000*\u0000__gadgetType":"EventGadget",

"\u0000*\u0000__gadgetDataType":"",

"description":"sadasdzxczxcasdsaweqqwasd",

"\u0000*\u0000__extendedPropertiesType":"EventExtendedProperties",

"\u0000*\u0000__extendedPropertiesDataType":"",

"\u0000*\u0000__endType":"EventDateTime",

"\u0000*\u0000__endDataType":"",

"end":{

"date":"",

"timeZone":"Europe\/Moscow",

"dateTime":"2012-04-08T19:00:00+04:00"

},

"\u0000*\u0000__remindersType":"EventReminders",

"\u0000*\u0000__remindersDataType":"",

"summary":"wqeqwesadasewqe"

}

注意:为了形成事件对象,我们使用了代码(与示例相同,例如developers.google.com/google-apps/calendar/v3/reference/events/insert部分示例)

Result: API returns an error with code 400 (Bad Request)

来自API的回答(带标题)

HTTP/1.1 400 Bad Request Content-Type: application/json; charset=UTF-8 Date: Fri, 06 Apr 2012 05:53:55 GMT Expires: Fri, 06 Apr 2012 05:53:55 GMT Cache-Control: private, max-age=0 X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block Server: GSE Transfer-Encoding: chunked 

{ "error": {

 "errors": [

 { "domain": "global",

 "reason": "badRequest",

 "message": "Bad Request" } 

],

 "code": 400, 

"message": "Bad Request"

 } 

}

2 个答案:

答案 0 :(得分:0)

玩这个工具。我有同样的问题。它适用于资源管理器,但不适用于我的rails服务器。

https://developers.google.com/google-apps/calendar/v3/reference/events/insert#try-it

答案 1 :(得分:0)

您提到的文档(在http://developers.google.com/google-apps/calendar/v3/reference上)记录了Google Calendar API的REST界面。但是,PHP客户端库的工作方式不同,遗憾的是很难记录。

我使用了Google PHP API客户端(https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/calendar/simple.php)附带的示例。您需要在Google的API控制台中生成oAuth客户端ID,密码等(如何在API客户端的文档中记录此工作)。

关于日历PHP API的所有其他文档都是(AFAIK),只能在Google_CalendarService类的评论中找到:https://code.google.com/p/google-api-php-client/source/browse/trunk/src/contrib/Google_CalendarService.php

您可以使用以下代码向Google日历添加新活动:

$event = new Google_Event();

// Event title and location
$event->setSummary('Event title');
$event->setLocation('Some location');

// Start and end time of the event
$start = new Google_EventDateTime();
$start->setDateTime('2013-08-08T14:00:00+02:00');
$event->setStart($start);

$stop = new Google_EventDateTime();
$stop->setDateTime('2013-08-08T16:00:00+02:00');
$event->setEnd($stop);

// Insert event in calendar.
// 'primary' may be replaced with a full @group.calendar.google.com calendar ID.
$createdEvent = $cal->events->insert('primary', $event);