简体   繁体   中英

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.

    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?.
  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. 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.

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. You'll do this through the API Library of the Cloud Dashboard.

  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

  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.

  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.

  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.

See this StackOverflow post for a more complete discussion about the process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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