admin管理员组

文章数量:1287668

I am attempting to teach myself some JQuery/REST/Google API by putting together a simple page that does the following:

  1. Authenticates with Google
  2. Validates a token
  3. Gets a list of Google Calendars for the user
  4. List events for a selected calendar
  5. Add an event for a selected calendar

I have #1 through #4 working (although no doubt in an ugly manner), and am getting tripped up on #5. Here's the JQuery ajax call:

var url = '/[MY_CALENDAR_ID]/events?sendNotifications=false&access_token=[GOOGLE_API_TOKEN]';
            var data = { end: { dateTime: "2012-07-22T11:30:00-07:00" }
                            , start: { dateTime: "2012-07-22T11:00:00-07:00" }
                            , summary: "New Calendar Event from API"
                        };

            var ajax = $.ajax({ url: url
                                , data: data
                                , type: 'POST'
                        }).done(addEventDone)
                          .fail(function (jqHXR, textStatus) {
                              console.log("addEvent(): ajax failed = " + jqHXR.responseText);
                              console.log(jqHXR);
                          });

The results are a global parseError: "This API does not support parsing form-encoded input.". The first four steps are all using GET ajax calls, so I'm not sure if that is what is tripping me up.

Here's the API in question:

I think I may be doing things the long and hard way, and my new approach is to tackle this using the javascript API instead of going straight at it with manual JQuery and REST. This is the approach I am attempting going forward , although I would still love to use this as a learning opportunity if there is something simple I am screwing up in the code above.

Thanks for any help, insights, pointers, etc. I will post updates if I make any progress using the javascript API.

I am attempting to teach myself some JQuery/REST/Google API by putting together a simple page that does the following:

  1. Authenticates with Google
  2. Validates a token
  3. Gets a list of Google Calendars for the user
  4. List events for a selected calendar
  5. Add an event for a selected calendar

I have #1 through #4 working (although no doubt in an ugly manner), and am getting tripped up on #5. Here's the JQuery ajax call:

var url = 'https://www.googleapis./calendar/v3/calendars/[MY_CALENDAR_ID]/events?sendNotifications=false&access_token=[GOOGLE_API_TOKEN]';
            var data = { end: { dateTime: "2012-07-22T11:30:00-07:00" }
                            , start: { dateTime: "2012-07-22T11:00:00-07:00" }
                            , summary: "New Calendar Event from API"
                        };

            var ajax = $.ajax({ url: url
                                , data: data
                                , type: 'POST'
                        }).done(addEventDone)
                          .fail(function (jqHXR, textStatus) {
                              console.log("addEvent(): ajax failed = " + jqHXR.responseText);
                              console.log(jqHXR);
                          });

The results are a global parseError: "This API does not support parsing form-encoded input.". The first four steps are all using GET ajax calls, so I'm not sure if that is what is tripping me up.

Here's the API in question: https://developers.google./google-apps/calendar/v3/reference/events/insert

I think I may be doing things the long and hard way, and my new approach is to tackle this using the javascript API instead of going straight at it with manual JQuery and REST. This is the approach I am attempting going forward http://code.google./p/google-api-javascript-client/wiki/Samples#Calendar_API, although I would still love to use this as a learning opportunity if there is something simple I am screwing up in the code above.

Thanks for any help, insights, pointers, etc. I will post updates if I make any progress using the javascript API.

Share Improve this question edited Jul 24, 2012 at 0:29 Dan Holevoet 9,1831 gold badge35 silver badges49 bronze badges asked Jul 22, 2012 at 19:43 ErikErik 3142 gold badges5 silver badges10 bronze badges 1
  • Does this answer your question? Need good example: Google Calendar API in Javascript – Liam Commented Dec 24, 2021 at 16:14
Add a ment  | 

2 Answers 2

Reset to default 5

Interestingly, I just answered a similar question here. Your intuition to use Google's JS client library is a good one. It's going to handle OAuth 2 for you, which is a requirement if you're going to do any manipulation of the Calendar data.

My other answer has both a link to a blog post that I authored (which demonstrates configuration of the client and user authorization), as well as an example of inserting a Calendar event.

you need to set contentType: "application/json", to do JSON.stringify for your data and method : 'POST'

var ajax = $.ajax({
url: url,
contentType: "application/json",
data: JSON.stringify(data),
method : 'POST',
});

本文标签: javascriptHow can I add an event to a Google calendar using v3 API and JQueryStack Overflow