Google 操作 - 访问日历 api 以插入事件列表

Google Actions-Access to calendar api to insert list of events

用户已经使用 google 从 Google 登录授权他的 gmail 帐户 Actions.Now 我需要向用户插入事件列表 google 日历,但是我遇到了一些问题或不知道如何构建 it.I 我对日历很陌生 api 所以请任何人指导我如何解决它。

    const {google} = require('googleapis');
        var calendar = google.calendar('v3');
        const SCOPES = ['https://www.googleapis.com/auth/calendar'];
        const client_secret = "xyz"; 
        const client_id     = "xyz";
        const redirect_uris ="xyz";
        const oAuth2Client = new google.auth.OAuth2(
          client_id, client_secret, redirect_uris);
        oAuth2Client.setCredentials({
         access_token: 'ACCESS TOKEN HERE'
       });
var event = {
  'summary': 'Google I/O 2015',
  'location': '800 Howard St., San Francisco, CA 94103',
  'description': 'A chance to hear more about Google\'s developer products.',
  'start': {
    'dateTime': '2015-05-28T09:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'end': {
    'dateTime': '2015-05-28T17:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
};
calendar.events.insert({
  auth: oAuth2Client,
  calendarId: 'primary',
  resource: event,
}, function(err, event) {
  if (err) {
    console.log('There was an error contacting the Calendar service: ' + err);
    return;
  }
});
  1. 如何使用从 google 操作接收到的 idtoken 访问用户的 google 日历?
  2. 如何向用户的日历中插入多个事件?

您使用 Google 登录助手获得的 ID 令牌不足以让您访问他们的日历。您将需要一个 访问令牌 或授权令牌。虽然 Google 登录对此有所帮助 - 这不是完整的图片,而且解决方案可能有点复杂。

一般来说,您需要执行以下操作:

  1. 您需要确保您用于智能助理的 Google 云项目已打开日历 API。您将通过云仪表板的 API Library 执行此操作。

  2. 您还需要为 Web 应用程序(诚实)创建一个 OAuth 2.0 客户端 ID 密钥,您可以通过 Cloud DashboardCredentials 页面

  3. 通过这些和 Google 登录,您可以创建一个 hybrid sign-in strategy 让用户登录并授权您的操作使用正确的范围。

  4. 在登录过程结束时,您将(最终)获得访问令牌和刷新令牌。您将使用用户的 Google ID 作为键将其存储在某种类型的数据存储或数据库中(例如 Firebase Firestore)。

  5. 然后,当他们访问您的操作时,您可以从 ID Token 中获取他们的 Google ID,在您的数据库中查找他们的访问令牌,并执行日历命令。

有关该过程的更完整讨论,请参阅