Javascript 如何使用 v3 API 和 JQuery 将事件添加到 Google 日历?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11603434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:30:16  来源:igfitidea点击:

How can I add an event to a Google calendar using v3 API and JQuery?

javascriptjquerygoogle-calendar-api

提问by Erik

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

我试图通过组合一个执行以下操作的简单页面来自学一些 JQuery/REST/Google API:

  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
  1. 通过 Google 进行身份验证
  2. 验证令牌
  3. 获取用户的 Google 日历列表
  4. 列出所选日历的事件
  5. 为所选日历添加事件

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:

我有 #1 到 #4 工作(尽管毫无疑问是以一种丑陋的方式),并且我被 #5 绊倒了。这是 JQuery ajax 调用:

var url = 'https://www.googleapis.com/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.

结果是一个全局的 parseError:“此 API 不支持解析表单编码的输入。”。前四个步骤都使用 GET ajax 调用,所以我不确定这是否是我绊倒的原因。

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

这是有问题的 API:https: //developers.google.com/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.com/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.

我想我做事的方式可能漫长而艰难,我的新方法是使用 javascript API 来解决这个问题,而不是直接使用手动 JQuery 和 REST 来解决这个问题。这是我尝试前进的方法http://code.google.com/p/google-api-javascript-client/wiki/Samples#Calendar_API,尽管如果有的话,我仍然希望将其用作学习机会我在上面的代码中搞砸了一些简单的事情。

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

感谢您的任何帮助、见解、指针等。如果我使用 javascript API 取得任何进展,我会发布更新。

回答by Dan Holevoet

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.

有趣的是,我刚刚在这里回答了一个类似的问题。您使用 Google 的 JS 客户端库的直觉很好。它将为您处理 OAuth 2,如果您要对日历数据进行任何操作,这是一项要求。

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

我的另一个答案包含指向我撰写的博客文章的链接(演示客户端和用户授权的配置),以及插入日历事件的示例。

回答by elad gasner

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

你需要设置 contentType: "application/json", 为你的数据和方法做 JSON.stringify : 'POST'

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