将节点应用程序连接到Google Calendar API

对于我而言,以某种方式出乎意料的是,将我的nodejs应用程序连接到Google Calendar API确实是一项不平凡的任务。尽管对俄语中的连接选项进行了详细的描述,但我还是不得不遍历各种设置和配置的目录林。本文详细介绍了成功进行集成所必须采取的步骤。

集成的目标是使nodejs应用程序能够将事件发布到特定的日历。在此示例中,我们使用了常规的个人Google帐户。

创建日历

首先,您需要创建一个日历,我们将在其中发布事件。转到Google日历,然后单击其他日历旁边的“ +按钮,然后选择“创建日历

填写表格,然后再次单击“创建日历”,但带有蓝色按钮:

谷歌已经动摇了很长时间,然后高兴地宣布新日历已经准备就绪。访问新的日历设置:

在设置中,我们主要对“日历集成”项感兴趣

其中最有用的是“日历ID

c093hr4fqjuj5k9e6uvvac73ac@group.calendar.google.com

nodejs- API.

Google

" API".

- " ":

- "Habr Demo":

API Google':

Google 3 , :

"calend" , :

"Google Calendar API" :

dashboard API (https://console.developers.google.com/apis/api/calendar-json.googleapis.com/overview?project=habr-demo-293107&supportedpurview=project), , API :

" " , , API:

, :

Google JSON, Google Calendar API:

"". , Google' , API, :

, " / ", . "" JSON- "Downloads" :

JSON- ( ):

{
  "type": "service_account",
  "project_id": "habr-demo-293107",
  "private_key_id": "4ec17ea5f8b606e0535a0623a110111123fd3c33",
  "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
  "client_email": "nodejs-app@habr-demo-293107.iam.gserviceaccount.com",
  "client_id": "102219376121816220804",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/nodejs-app%40habr-demo-293107.iam.gserviceaccount.com"
}

API "Habr Demo" email' "nodejs-app@habr-demo-293107.iam.gserviceaccount.com":

API nodejs- Google googleapis . OAuth2- , scope' . :

const fs = require('fs');
const {google} = require('googleapis');

const CALENDAR_ID = 'c093hr4fqjuj5k9e6uvvac73ac@group.calendar.google.com';
const KEYFILE = 'Habr Demo-4ec17ea5f8b6.json'; // path to JSON with private key been downloaded from Google
const SCOPE_CALENDAR = 'https://www.googleapis.com/auth/calendar'; // authorization scopes
const SCOPE_EVENTS = 'https://www.googleapis.com/auth/calendar.events';

(async function run() {
    // INNER FUNCTIONS
    async function readPrivateKey() {
        const content = fs.readFileSync(KEYFILE);
        return JSON.parse(content.toString());
    }

    async function authenticate(key) {
        const jwtClient = new google.auth.JWT(
            key.client_email,
            null,
            key.private_key,
            [SCOPE_CALENDAR, SCOPE_EVENTS]
        );
        await jwtClient.authorize();
        return jwtClient;
    }

    async function createEvent(auth) {
        const event = {
            'summary': 'Habr Post Demo',
            'description': '    nodejs-  Google Calendar API.',
            'start': {
                'dateTime': '2020-10-20T16:00:00+02:00',
                'timeZone': 'Europe/Riga',
            },
            'end': {
                'dateTime': '2020-10-20T18:00:00+02:00',
                'timeZone': 'Europe/Riga',
            }
        };

        let calendar = google.calendar('v3');
        await calendar.events.insert({
            auth: auth,
            calendarId: CALENDAR_ID,
            resource: event,
        });
    }

    // MAIN
    try {
        const key = await readPrivateKey();
        const auth = await authenticate(key);
        await createEvent(auth);
    } catch (e) {
        console.log('Error: ' + e);
    }
})();

Calendar API:

{
  ...
  "status": 404,
  "statusText": "Not Found",
  ...
}

- .

, , " ", " " email- , :

:

" ", "You need to have writer access to this calendar." API:

:

16:00:

'start': {
    'dateTime': '2020-10-20T16:00:00+02:00',
    'timeZone': 'Europe/Riga',
}

a 17:00, IT:

编程中只有两个典型的困难:缓存失效,实体名称和每单位错误

概要

就这样,任务完成了。正如他们所说,编码很快乐。

链接




All Articles