我正在尝试使用Google Calendar API v3创建日历(如果它尚不存在)。我的实现成功检索了我的所有日历和事件,并可以更改日历,但我正在努力添加新日历。这就是我为了尝试为用户添加新日历而做的事情:
...
var calendarService = new CalendarService(_authenticator);
var calendarId = "com.somedomain.somename.1234"; // Some random ID
try {
calendarManager.GetCalendar(calendarId); // No calandar found
} catch(GoogleCalandarServiceProxyException e){
// Ok, so far so good, calendar not found (that is correct) and I am here
var someCalendar = new Google.Apis.Calendar.v3.Data.CalendarListEntry {
Summary = "Some summary!",
Description = "A calendar descr.",
AccessRole = "writer",
BackgroundColor = "#E7323C",
Id = calendarId
};
calendarService.CalendarList.Insert(someCalendar).Fetch(); // Fetch to execute
}
请求已生成:
insert @ https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json&prettyPrint=true
RequestError:
Google.Apis.Request.RequestErrorNotFound[404]Errors [Message[Not Found]Location[-] Reason[notFound] Domain[global]]]
奇怪的是,在https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json&prettyPrint=true进行GET不会返回某些内容,所以应该在那里。
我希望有一些很棒的家伙/女孩知道该怎么做!我完全卡住了。
更新 好像我在CalendarList Google Api Explorer上也收到了同样的404错误:
网址: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/insert
请求:
POST https://www.googleapis.com/calendar/v3/users/me/calendarList?key={YOUR_API_KEY}
Content-Type: application/json
Authorization: Bearer ya29.AHES6ZQTj-D6uIMOWr_AoFYVvfefsEGcVvLvvMf4aKhgrdvQE25aVw
X-JavaScript-User-Agent: Google APIs Explorer
{
"id": "hastalavista"
}
响应:
404 Not Found
- Show headers - { "error": { "errors": [ {
"domain": "global",
"reason": "notFound",
"message": "Not Found" } ], "code": 404, "message": "Not Found" } }
有谁知道Google是否更改了此资源的网址?如果是,我如何更改Google Calendar Api v3以使用更新后的URL ...?
答案 0 :(得分:11)
您无法使用自己的标识符创建日历,CalendarListEntry.Insert()的目的是将创建的日历插入日历列表
因此要创建新日历,必须:
插入新日历:https://developers.google.com/google-apps/calendar/v3/reference/calendars/insert
(不要将id添加为参数,这将自动创建)
插入在步骤1中创建的日历的ID,并将其插入calendarList:https://developers.google.com/google-apps/calendar/v3/reference/calendarList/insert
答案 1 :(得分:1)
由于数据似乎重叠,我对Google在Calendar和CalendarList之间的区别仍感到困惑。为什么日历被视为“次要”日历。
答案 2 :(得分:0)
接下来的代码对我来说就是诀窍,上面提到的参考文献是Java而不是.Net,并且像往常一样是一些令人讨厌的差异......
private bool createCalendar() {
// use Google.Apis.Calendar
Calendar calendar = new Calendar();
calendar.Summary = "MyNewCalendar";
calendar.Description = "Your new Calendar";
calendar.Location = "Aadorp";
calendar.TimeZone = "Europe/Amsterdam";
try
{
// Not inserting in CalendarList but in Calendar!
calendarService.Calendars.Insert(calendar).Execute();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
return false;
}
return true;
}