带 Nodejs 的 Google API 日历

问题描述 投票:0回答:1

我想让它做什么

在客户预订时创建日历事件。日历是单个员工拥有的日历(例如:

[email protected]

我已经成功地将 api 与我的 NextJS 应用程序链接到服务器到服务器服务帐户 方法。当我创建一个日历事件时,我可以在终端中取回数据。当我去登录我的帐户时,用户界面仍然是空白

我确定了

  • 相对于今年创建的日期,很容易围绕今天的日期
  • 我要把它发送到“主”日历

我是否认为创建 Google Cloud Console 项目 的帐户没有自动链接到这些 api 调用是错误的?

来自帐户所有者的日历 UI

我尝试使用

+
按钮添加一个新日历,例如
[email protected]
并得到错误

您无权访问 [email protected] 的日历。

请求访问

我应该如何访问这封

.gserviceaccount.com
电子邮件?

我试过的

在我从 api 创建了一些事件之后,我可以返回该数据

/pages/api/google/create.ts

calendar.events.list({
    auth: jwtClient,
    calendarId: GOOGLE_CAL_ID
  }, function (err, response) {

    if(response) {
      console.log('----- here is responses ---- ');
      
      return console.log(response.data.items)
    };
    if(err) return console.log(err);
    
  });

终端响应

[
  {
    kind: 'calendar#event',
    etag: '"3366666430186000"',
    id: '5rbkghab02095kgoj0oc92u3ko',
    status: 'confirmed',
    htmlLink: 'https://www.google.com/calendar/event?eid=NXJia2doYWIwMjA5NWtnb2owb2M5MnUza28gY3V0ZWZydWl0LWNhbGVuZGVyQGN1dGVmcnVpdC1wcm9qZWN0LmlhbS5nc2VydmljZWFjY291bnQuY29t',
    created: '2023-05-06T00:33:35.000Z',
    updated: '2023-05-06T00:33:35.093Z',
    summary: 'Event 1',
    description: 'Sample description',
    creator: {
      email: '[email protected]',
      self: true
    },
    organizer: {
      email: '[email protected]',
      self: true
    },
    start: { dateTime: '2023-05-01T05:00:00Z', timeZone: 'America/Chicago' },
    end: { dateTime: '2023-05-01T06:00:00Z', timeZone: 'America/Chicago' },
    iCalUID: '[email protected]',
    sequence: 0,
    reminders: { useDefault: true },
    eventType: 'default'
  },
  ...,
  ...,
]

完整文件

import type { NextApiRequest, NextApiResponse } from 'next'

import { google } from 'googleapis';
import { JWT } from 'google-auth-library';
import creds from "../../../private/cutefruit-project.json";

const scopes = ['https://www.googleapis.com/auth/calendar'];
const GOOGLE_PRIVATE_KEY = creds.private_key|| 'NO_KEY_SET'
const GOOGLE_CLIENT_EMAIL = creds.client_email || 'NO_EMAIL_SET'
const GOOGLE_CAL_ID = 'primary'

let jwtClient = new JWT({
  email: GOOGLE_CLIENT_EMAIL,
  key: GOOGLE_PRIVATE_KEY,
  scopes,
})

//authenticate request
jwtClient.authorize(function (err, tokens) {
  console.log('JWT AUTHORIZATION');
  
  if (err) {
    console.log(err);
    return;
  } else {
    console.log("Successfully connected!")
  }
})

type Data = {
  name: string,
  data: any,
}

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<Data>
) {

  const event = req.body

  let calendar = google.calendar('v3')

  calendar.events.insert({
    auth: jwtClient,
    calendarId: 'primary',
    requestBody: {
      'summary': 'Event 3',
      'description': 'Sample description',
      'start': {
        'dateTime': '2023-05-03T00:00:00',
        'timeZone': 'America/Chicago',
      },
      'end': {
        'dateTime': '2023-05-03T01:00:00',
        'timeZone': 'America/Chicago',
      },
    },
  }, function(err, res) {
    if (err) {
      console.log('Error: ' + err);
      return;
    }
    console.log(res?.status);
  })


  res.status(200).json({ name: 'cal event created', data: 'message response' })
}

reactjs google-api google-calendar-api
1个回答
0
投票

回答我自己的问题

这篇文章帮助我理解了通过服务帐户设置api的夸克

  1. 在您的个人谷歌帐户中,创建一个新日历
  2. 将日历共享到在谷歌云控制台中生成的服务帐户电子邮件。 (例如:
    [email protected]
  3. 复制那个日历的
    Calendar ID
    (例如:
    bda948b786f16be2d763e0b2d3c699bc8230643f96bfbfb2at64b66e7fc92b11@group.calendar.google.com
  4. 在您的 api 调用中使用该日历 ID
calendar.events.insert({
  auth: jwtClient,
  calendarId: GOOGLE_CAL_ID, // <-- here
  requestBody: event,
})
© www.soinside.com 2019 - 2024. All rights reserved.