Error 404 when creating a calendar with Google Calendar Api v3 using c# .net

谁说胖子不能爱 提交于 2019-12-07 06:13:56

问题


I am trying to create a calendar using Google Calendar API v3 if it does not already exist. My implementation successfully retrieves all my calendars and events and can change calendars, but I am struggling with adding a new calendar. This is what I do in order to try to add a new calendar for the user:

...
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
}

Request generated:

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]]]

Weird thing is that doing a GET at https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json&prettyPrint=true does not return something, so it should be there.

I hope that there is some awesome guy/girl out there which knows what to do! I'm completely stuck.

Update Seems like I am receiving the same 404 error on CalendarList Google Api Explorer also:

Url: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/insert

Request:

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"
}

Response:

404 Not Found

- Show headers -    {  "error": {   "errors": [    {
    "domain": "global",
    "reason": "notFound",
    "message": "Not Found"    }   ],   "code": 404,   "message": "Not Found"  } }

Does anyone know whether Google have changed the URL for this resource? And if yes, how I change the Google Calendar Api v3 to use the updated url ...?


回答1:


Okey, my bad:

You can't create a calendar with your own identifier, the purpose of CalendarListEntry.Insert() is to insert a created Calendar into the list of calendars

So to create a new calendar one has to:

  1. Insert a new Calendar: https://developers.google.com/google-apps/calendar/v3/reference/calendars/insert

    (DO NOT add id as paramater, this will be automatically created)

  2. Insert the ID of the calendar created in step 1 and insert it into the calendarList: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/insert




回答2:


I'm still totally confused as to Google's distinction between a Calendar and a CalendarList since the data seems to overlap. Why is a calendar considered a "secondary" calendar.




回答3:


The next code dit the trick for me, The mentioned reference is Java not .Net and, as usual ther e are some anoying differences...

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;
    }


来源:https://stackoverflow.com/questions/17865302/error-404-when-creating-a-calendar-with-google-calendar-api-v3-using-c-sharp-ne

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!