当前位置: 首页 > 知识库问答 >
问题:

错误:缺少结束时间。带有节点JS的Google日历API

扶冠宇
2023-03-14

我正在实现谷歌日历API指令网页的例子。我将代码放入example.js文件中,并在命令行中用节点example.js启动它。

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), listEvents);
});

/**
 * 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 **strong text**{
    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);
}

/**
 * Lists the next 10 events on the user's primary calendar.
 *
 * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
 */
function listEvents(auth) {
  var calendar = google.calendar('v3');
  calendar.events.list({
    auth: auth,
    calendarId: 'primary',
    timeMin: (new Date()).toISOString(),
    maxResults: 10,
    singleEvents: true,
    orderBy: 'startTime'
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
    var events = response.items;
    if (events.length == 0) {
      console.log('No upcoming events found.');
    } else {
      console.log('Upcoming 10 events:');
      for (var i = 0; i < events.length; i++) {
        var event = events[i];
        var start = event.start.dateTime || event.start.date;
        console.log('%s - %s', start, event.summary);
      }
    }
  });
}
  authorize(JSON.parse(content), addEvent);

function addEvent(auth) {
    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',
      },
      'recurrence': [
        'RRULE:FREQ=DAILY;COUNT=2'
      ],
      'attendees': [
        {'email': 'lpage@example.com'},
        {'email': 'sbrin@example.com'},
      ],
      'reminders': {
        'useDefault': false,
        'overrides': [
          {'method': 'email', 'minutes': 24 * 60},
          {'method': 'popup', 'minutes': 10},
        ],
      },
    };

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);
    });

联系日历服务时出错:错误:缺少结束时间。

我做错了什么。

共有1个答案

艾子石
2023-03-14

我也有同样的问题。我有一个版本的google-auth-library与最新的GooglePis版本不兼容。检查您的版本-您可能需要升级和迁移到您的google-auth-library,这就是为什么上面的示例对您不起作用。

如果这是您的问题,您可能需要做以下编辑:https://github.com/google/google-auth-library-nodejs/releases/tag/v1.0.0

 类似资料:
  • endpoint=https://www.googleapis.com/calendar/v3/calendars/George.albrecht%40xxx.com/events,method=get

  • 问题内容: 这是我的代码,用于查看从ListActivity中选择的事件(是包含所有这些事件的ArrayList): 此代码对重复发生的事件以外的所有事件均适用。对于重复发生的任何事件,endTime返回为null,从而导致程序崩溃。有人知道如何解决这个问题吗?我还有其他需要通过的额外费用吗? 问题答案: beginTime和endTime可以为0 / null,因为您是从错误的数据库(当然是从

  • 问题内容: 编辑:在这段代码上发生错误: 我正在使用Node js的gmail api。当我阅读他们的快速入门指南时,我会不断收到此错误。 https://developers.google.com/gmail/api/quickstart/nodejs 请记住,我使用电子邮件地址进行了快速入门,一切都很好。完全没有错误。我决定为测试目的创建一个虚拟电子邮件。我所做的唯一更改是关闭了文件以保留新的

  • 问题内容: 我在数据库中创建2个表: 和: 当我在沼泽中运行查询时,出现此错误: SQL查询: 说:文档 #1822-无法添加外键约束。参照表“药物”中约束“ fk_med_pharmacy_medication1”的缺少索引 这些表已经存在,但是我更改了一个字段。 问题答案: 外索引中引用的列必须被索引。您需要在添加索引。实际上,这可能应该是表的主键。

  • 在设置我的连接之后 和我的服务器页面如下所示: const mongoose=require(“mongoose”)const app=new express()

  • 问题内容: 如何使用google-api-java-client解析用户Google日历中事件的开始和结束时间? 从Google代码安装此示例android项目后,我可以进入Google日历并解析一些信息(例如所有日历,事件名称,发布时间和摘要),但是我无法终生获取事件的开始和结束时间。 我对代码的理解是这样的。 在主要活动类(CalendarAndroidSample.java)内,这是获取我每