ノードアプリケーションをGoogleCalendarAPIに接続する

どういうわけか、私にとって予期せぬことに、nodejsアプリケーションをGoogle Calendar APIに接続することは、かなり簡単な作業ではないことがわかりました。ロシア語での接続オプションの詳細な説明にもかかわらず、私はさまざまな設定と構成の森を歩き回らなければなりませんでした。この記事では、統合を成功させるために実行する必要のある手順について詳しく説明します。

統合の目的は、nodejsアプリケーションがイベントを特定のカレンダーに公開できるようにすることです。この例では、通常の個人用Googleアカウントを使用しました。

カレンダーを作成する

まず、イベントを公開するカレンダーを作成する必要があります。移動し、Googleカレンダーと「をクリックしてください+ボタン「の隣にある他のカレンダー」を選択し、その後、「カレンダーの作成の項目は

フォームに記入して[カレンダーの作成]をもう一度押しますが、今回は青いボタン:

グーグルは長い間頭を悩ませてきましたが、その後、新しいカレンダーの準備ができたことを喜んで発表します。新しいカレンダー設定へのアクセス:

" ":

- " "

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:

プログラミングには、キャッシュの無効化、エンティティ名、およびユニットごとのエラーという2つの一般的な問題しかありません。

概要

以上で、クエストは完了です。彼らが言うように、コーディングは幸せです。

リンク




All Articles