简体   繁体   English

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

[英]Google Actions-Access to calendar api to insert list of events

The user has been already authorized his gmail account using google sign in from Google Actions.Now i need to insert list of events to users google calendar,but i'm facing some issue or not aware of how to build it.I'm very new to calendar api so please any one guide me how to resolve it.用户已经使用 Google Actions 的 google 登录获得了他的 gmail 帐户的授权。现在我需要向用户的 google 日历插入事件列表,但我面临一些问题或不知道如何构建它。我非常日历 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. How to access user's google calendar by using received idtoken from google actions?.如何使用从 google 操作收到的 idtoken 访问用户的 google 日历?
  2. How to insert multiple events to user's calendar?.如何在用户的日历中插入多个事件?

The id token that you'll get by using Google Sign In for Assistant isn't enough to get you access to their calendar.您使用 Google Sign In for Assistant 获得的 id 令牌不足以让您访问他们的日历。 You will need an access token or auth token.您将需要访问令牌或身份验证令牌。 While Google Sign In helps with this - it isn't the complete picture, and the solution can be a bit convoluted.虽然 Google Sign In 对此有所帮助 - 它不是完整的图片,并且解决方案可能有点复杂。

In general, you'll need to do the following:通常,您需要执行以下操作:

  1. You need to make sure the Google Cloud project you're using for your Assistant has the Calendar APIs turned on.您需要确保用于智能助理的 Google Cloud 项目已开启日历 API。 You'll do this through the API Library of the Cloud Dashboard.您将通过 Cloud Dashboard 的API 库执行此操作。

  2. You will also need to create an OAuth 2.0 client id key for a web application (honest) that you do through the Credentials page of the Cloud Dashboard您还需要通过 Cloud Dashboard 的Credentials页面为 Web 应用程序(诚实)创建 OAuth 2.0 客户端 ID 密钥

  3. With these, and with Google Sign In, you can create a hybrid sign-in strategy that will let the user log in and authorize your Action to get access their calendar using the correct scopes.有了这些,再加上 Google 登录,您可以创建混合登录策略,让用户登录并授权您的操作使用正确的范围访问他们的日历。

  4. At the end of the login process, you will (eventually) get an access token and refresh token.在登录过程结束时,您将(最终)获得访问令牌和刷新令牌。 You'll store this in a datastore or database of some sort (such as Firebase Firestore) with the user's Google ID as the key.您将使用用户的 Google ID 作为键将其存储在某种类型的数据存储区或数据库(例如 Firebase Firestore)中。

  5. Then, when they visit your action, you can get their Google ID from the ID Token, look up their access token in your database, and execute the calendar command.然后,当他们访问您的操作时,您可以从 ID Token 中获取他们的 Google ID,在您的数据库中查找他们的访问令牌,并执行日历命令。

See this StackOverflow post for a more complete discussion about the process.有关过程的更完整讨论,请参阅此 StackOverflow 帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM