从节点服务器访问Google Calendar API [英] Accessing Google Calendar API from Node server

查看:56
本文介绍了从节点服务器访问Google Calendar API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我很难访问Google日历.

For some reason, I am having a really hard time accessing Google calendar.

我希望能够从Node.js服务器中添加和删除日历中的事件.

I want to be able to add and remove events in a calendar from my Node.js server.

我从文档中发现确实有冲突的信息.

I am finding really conflicting information from the documents.

我关注了- https://developers.google.com/identity/protocols/OAuth2ServiceAccount为如何获得访问令牌提供了很好的指南,但最后似乎只用于访问云端硬盘.

I followed - https://developers.google.com/identity/protocols/OAuth2ServiceAccount which gives a good guide on how to get an acccess token but then at the end it appears it is only for accessing Drive.

然后我关注了未配置Google Calendar API v3访问指出您只需要一个API密钥,但这看起来好像都是从客户端完成的,所以可能有所不同吗?

I then followed Google Calendar API v3 Access Not Configured which states you only need an API key but this appears as though it is all done from client side so maybe it's different?

我查看了 https://developers.google.com/google-apps/calendar/quickstart/nodejs 也是如此,但仅对日历进行简单的API调用似乎非常复杂.示例代码引用的文件不清楚,这些文件来自何处或如何构造.例如. var TOKEN_PATH = TOKEN_DIR +'calendar-nodejs-quickstart.json';

I looked at https://developers.google.com/google-apps/calendar/quickstart/nodejs as well but that seems very complicated just to make a simple API call to a calendar. The sample code references files which isn't clear where they come from or how to structure them. E.g. var TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json';

我非常赞赏如何实现这一目标的简单指南.

I simple guide on how to achieve this would be hugely appreciated.

谢谢

推荐答案

我相信您想使用Node.js在日历中添加和删除事件.

I believe you want to add and remove events in a calendar using Node.js.

关于使用日历API的 quickstart.js ,为了使用日历API,首先,用户必须检索具有客户端ID,客户端密码和等等,然后在API控制台上启用日历API.

About quickstart.js for using calendar API, in order to use calendar API, at first, users have to retrieve client_secret.json with client ID, client secret and so on, and enable calendar API at API console.

下一步,必须使用 client_secret.json 从Google检索访问令牌和刷新令牌.快速入门中的大多数 quickstart.js 用于检索它们. var TOKEN_PATH = TOKEN_DIR +'calendar-nodejs-quickstart.json'; 包括使用 client_secret.json 检索的访问令牌和刷新令牌.可以从不具有到期时间的刷新令牌中检索具有到期时间的访问令牌.在 quickstart.js ,使用刷新令牌在每次运行脚本时检索访问令牌.

As a next step, access token and refresh token have to be retrieved from Google using client_secret.json. Most of quickstart.js in Quickstart is used to retrieve them. var TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json'; includes the access token and refresh token retrieved using client_secret.json. The access token which has the expiration time can be retrieved from the refresh token which doesn't have the expiration time. At quickstart.js, access token is retrieved every running the script using the refresh token.

quickstart.js 中的 listEvents(auth)以外的功能均用于授权.在 listEvents(auth)处,可以使用授权检索的访问令牌来使用日历API.

Functions except for listEvents(auth) in the quickstart.js are used for the authorization. At listEvents(auth), calendar API can be used by using the access token retrieved by the authorization.

以下示例脚本用于添加和删除事件.假设快速入门中的步骤1和步骤2已经完成,并且使用了 quickstart.js .

Following sample script is for adding and removing events. This supposes that Step 1 and Step 2 in Quickstart have already finished and quickstart.js is used.

对于 Node.js快速入门示例,它进行了修改 listEvents().使用此示例脚本时,请复制并粘贴 Node.js快速入门示例,并按如下所示更改 listEvents(),然后添加以下 addEvents() removeEvents().

For Node.js Quickstart sample, it modified listEvents(). When you use this sample script, please copy and paste Node.js Quickstart sample and change listEvents() as follows, and add following addEvents() and removeEvents().

function listEvents(auth) {
  var calendar = google.calendar('v3');

  addEvents(auth, calendar); // Add events
  removeEvents(auth, calendar); // Remove events
}

1.添加事件

详细信息为 https://developers.google.com/google-apps/calendar/v3/reference/events/insert .

function addEvents(auth, calendar){
  calendar.events.insert({
    auth: auth,
    calendarId: 'primary',
    resource: {
      'summary': 'Sample Event',
      'description': 'Sample description',
      'start': {
        'dateTime': '2017-01-01T00:00:00',
        'timeZone': 'GMT',
      },
      'end': {
        'dateTime': '2017-01-01T01:00:00',
        'timeZone': 'GMT',
      },
    },
  }, function(err, res) {
    if (err) {
      console.log('Error: ' + err);
      return;
    }
    console.log(res);
  });
}

2.删除事件

详细信息为 https://developers.google.com/google-apps/calendar/v3/reference/events/delete .

function removeEvents(auth, calendar){
  calendar.events.delete({
    auth: auth,
    calendarId: 'primary',
    eventId: "#####",
  }, function(err) {
    if (err) {
      console.log('Error: ' + err);
      return;
    }
    console.log("Removed");
  });
}

这篇关于从节点服务器访问Google Calendar API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆