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

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

用户已经使用Google操作中的谷歌登录授权他的gmail帐户。现在我需要向用户谷歌日历插入事件列表,但我面临一些问题或者不知道如何构建它。我很新的日历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. 如何使用谷歌行动收到的idtoken访问用户的谷歌日历?
  2. 如何将多个事件插入用户的日历?
actions-on-google google-calendar-api
1个回答
2
投票

使用Google Sign In for Assistant获得的ID令牌不足以让您访问其日历。您将需要访问令牌或身份验证令牌。虽然谷歌登录有助于此 - 它不是完整的图片,解决方案可能有点复杂。

通常,您需要执行以下操作:

  1. 您需要确保为助手使用的Google Cloud项目已打开日历API。您将通过Cloud Dashboard的API Library执行此操作。
  2. 您还需要通过Cloud Dashboard的Credentials页面为Web应用程序(诚实)创建OAuth 2.0客户端ID密钥。
  3. 有了这些,并且通过Google登录,您可以创建一个hybrid sign-in strategy,让用户登录并授权您的操作使用正确的范围访问他们的日历。
  4. 在登录过程结束时,您将(最终)获得访问令牌并刷新令牌。您将以用户的Google ID作为密钥将其存储在某种数据存储区或数据库(例如Firebase Firestore)中。
  5. 然后,当他们访问您的操作时,您可以从ID令牌中获取他们的Google ID,在数据库中查找他们的访问令牌,然后执行calendar命令。

有关该过程的更完整的讨论,请参见this StackOverflow post

© www.soinside.com 2019 - 2024. All rights reserved.