Error: Missing end time. Google Calendar API with Node JS

前端 未结 4 1878
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-24 01:12

I am realising example from Google Calendar API instruction web page. I put the code into example.js file and start it with node example.js in command line.

<         


        
相关标签:
4条回答
  • 2021-01-24 01:22

    Try mine:

    var fs = require('fs');
    var readline = require('readline');
    var google = require('googleapis');
    var googleAuth = require('google-auth-library');
    
    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/calendar-nodejs-quickstart.json
    var SCOPES = ['https://www.googleapis.com/auth/calendar'];
    var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
        process.env.USERPROFILE) + '/.credentials/';
    var TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json';
    
    // Load client secrets from a local file.
    fs.readFile('client_secret.json', function processClientSecrets(err, content) {
      if (err) {
        console.log('Error loading client secret file: ' + err);
        return;
      }
      // Authorize a client with the loaded credentials, then call the
      // Google Calendar API.
      authorize(JSON.parse(content), insertEvent);
    });
    /**
     * Create an OAuth2 client with the given credentials, and then execute the
     * given callback function.
     *
     * @param {Object} credentials The authorization client credentials.
     * @param {function} callback The callback to call with the authorized client.
     */
    function authorize(credentials, callback) {
      var clientSecret = credentials.installed.client_secret;
      var clientId = credentials.installed.client_id;
      var redirectUrl = credentials.installed.redirect_uris[0];
      var auth = new googleAuth();
      var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
      // Check if we have previously stored a token.
      fs.readFile(TOKEN_PATH, function(err, token) {
        if (err) {
          getNewToken(oauth2Client, callback);
        } else {
          oauth2Client.credentials = JSON.parse(token);
          callback(oauth2Client);
        }
      });
    }
    /**
     * Get and store new token after prompting for user authorization, and then
     * execute the given callback with the authorized OAuth2 client.
     *
     * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
     * @param {getEventsCallback} callback The callback to call with the authorized
     *     client.
     */
    function getNewToken(oauth2Client, callback) {
      var authUrl = oauth2Client.generateAuthUrl({
        access_type: 'offline',
        scope: SCOPES
      });
      console.log('Authorize this app by visiting this url: ', authUrl);
      var rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
      });
      rl.question('Enter the code from that page here: ', function(code) {
        rl.close();
        oauth2Client.getToken(code, function(err, token) {
          if (err) {
            console.log('Error while trying to retrieve access token', err);
            return;
          }
          oauth2Client.credentials = token;
          storeToken(token);
          callback(oauth2Client);
        });
      });
    }
    /**
     * Store token to disk be used in later program executions.
     *
     * @param {Object} token The token to store to disk.
     */
    function storeToken(token) {
      try {
        fs.mkdirSync(TOKEN_DIR);
      } catch (err) {
        if (err.code != 'EEXIST') {
          throw err;
        }
      }
      fs.writeFile(TOKEN_PATH, JSON.stringify(token));
      console.log('Token stored to ' + TOKEN_PATH);
    }
    
    function insertEvent(auth) {
    
    var event = {
      'summary': 'This is a test Calendar invite using NodeJS',
      'location': 'Under the Bridge',
      'description': 'I dont ever wanna feel Like I did that day But take me to the place I love Take me all the way',
      'start': {
        'dateTime': '2018-02-25T12:58:05-07:00',
        'timeZone': 'America/Los_Angeles',
      },
      'end': {
        'dateTime': '2018-02-26T06:00:00-07:00',
        'timeZone': 'America/Los_Angeles',
      },
      'recurrence': [
        'RRULE:FREQ=DAILY;COUNT=2'
      ],
      'attendees': [
        {'email': 'rhcp@gmail.com'}
    
      ],
      'reminders': {
        'useDefault': true
      }
    };
    
    var calendar = google.calendar('v3');
    calendar.events.insert({
      auth: auth,
      calendarId: 'primary',
      resource: event,
    }, function(err, event) {
      if (err) {
        console.log('There was an error contacting the Calendar service: ' + err);
        return;
      }
      console.log('Event created: %s', event.htmlLink);
    });
    

    }

    Proof the sample Quickstart is working. Events are inserted in my calendar:

    0 讨论(0)
  • 2021-01-24 01:29

    I had the same issue. I had a version of the google-auth-library that wasn't compatible with the most recent googleapis version. Check your versions - you might need to upgrade and migrate over your google-auth-library which is why the example above doesnt work for you.

    Here's the edits you might have to do if this is your issue: https://github.com/google/google-auth-library-nodejs/releases/tag/v1.0.0

    0 讨论(0)
  • 2021-01-24 01:31

    I am currently having the same error as this one, using codes from examples and from @noogui. I found this library working as expected although requires a lot of configuration node-google-calendar.

    I also hope Google Calendar API will work soon.

    0 讨论(0)
  • 2021-01-24 01:32

    googleapis@24.0.0 and google-auth-library@0.12.0 worked for me

    0 讨论(0)
提交回复
热议问题