无法通过API在Google日历中插入新活动

时间:2015-02-03 18:32:26

标签: c# google-calendar-api google-oauth

我正在使用Google Calendar v3,OAuth2,我设法登录,查询事件Feed,但我无法通过api插入新事件。日历是公开的并且是共享的。这是我的示例代码:

AtomEntry entry = new AtomEntry();
AtomPerson author = new AtomPerson(AtomPersonType.Author);
author.Name = "xx";    author.Email = "xx@yahoo.com";
entry.Authors.Add(author);    entry.Title.Text = "Test";
entry.Content.Content = "Test";
calname = "678546u3ua4e1cgc2g1ugku1kg@group.calendar.google.com";
feedUrl = "https://www.googleapis.com/calendar/v3/calendars/{0}/events";
Uri postUri = new Uri(string.Format(feedUrl,calname));
AtomEntry insertedEntry = CalService.Insert(postUri, entry);

我收到此错误:

[WebException: The remote server returned an error: (401) Unauthorized.]
   System.Net.HttpWebRequest.GetResponse() +6542104
   Google.GData.Client.GDataRequest.Execute() +136

[GDataRequestException: Execution of request failed: https://www.googleapis.com/calendar/v3/calendars/678546u3ua4e1cgc2g1ugku1kg@group.calendar.google.com/events]
Can you please provide a working C# sample with all inclusive instructions on how to insert events from a web application after the user has given permission to use their calendar? Thanks!

1 个答案:

答案 0 :(得分:0)

检查您为OAuth 2.0访问令牌请求的范围。要写入日历,您需要的权限不同于只读。见:https://developers.google.com/google-apps/calendar/auth。您应该确保从日历所有者请求并获取范围https://www.googleapis.com/auth/calendar