繁体   English   中英

为什么在尝试联系日历时出现“需要登录”错误?

[英]Why I am getting error “login required” when trying to contact the calendar?

我正在使用Google API日历。 为什么我在下面的脚本中收到此错误消息?

联系日历时出错:需要登录

当前代码:

app.post('/token', function(req, res) {
  var code = req.body.code;
  console.log(code);
  async function asyncCall() {
    const { tokens } = await oauth2Client.getToken(code);
    oauth2Client.setCredentials(tokens);
    console.log(tokens);
  }
  asyncCall();
  oauth2Client.on('tokens', tokens => {
    if (tokens.refresh_token) {
      console.log(tokens.refresh_token);
    }
    console.log(tokens.access_token);
    console.log('Now lets see');
  });
  //
  addEvents();
  function addEvents(auth) {
    var event = {
      summary: 'Google I/O 2045',
      location: '800 Howard St., San Francisco, CA 94103',
      description: "A chance to hear more about Google's 
      developer products.",
      start: {
        dateTime: '2018-10-28T09:00:00-07:00',
        timeZone: 'America/Los_Angeles'
      },
      end: {
        dateTime: '2018-10-28T17:00:00-07:00',
        timeZone: 'America/Los_Angeles'
      },
      recurrence: ['RRULE:FREQ=DAILY;COUNT=2'],
      reminders: {
        useDefault: false,
        overrides: [
          { method: 'email', minutes: 24 * 60 },
          { method: 'popup', minutes: 10 }
        ]
      }
    };

    //console.log(event)

    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. auth应该在日历对象中分配给oauth2Client。

  2. 重新设计您的addEvents()。 您无需使用auth作为参数来定义addEvents函数。 同样,您必须返回var事件并将其分配给资源。 这应该工作:

app.post('/token', function(req, res) {
  var code = req.body.code;
  console.log(code);
  async function asyncCall() {
    const { tokens } = await oauth2Client.getToken(code);
    oauth2Client.setCredentials(tokens);
    console.log(tokens);
  }
  oauth2Client.on('tokens', tokens => {
    if (tokens.refresh_token) {
      console.log(tokens.refresh_token);
    }
    console.log(tokens.access_token);
    console.log('Now lets see');
  });

    function addEvents() {
    var event = {
      summary: 'Google I/O 2045',
      location: '800 Howard St., San Francisco, CA 94103',
      description: "A chance to hear more about Google's 
      developer products.",
      start: {
        dateTime: '2018-10-28T09:00:00-07:00',
        timeZone: 'America/Los_Angeles'
      },
      end: {
        dateTime: '2018-10-28T17:00:00-07:00',
        timeZone: 'America/Los_Angeles'
      },
      recurrence: ['RRULE:FREQ=DAILY;COUNT=2'],
      reminders: {
        useDefault: false,
        overrides: [
          { method: 'email', minutes: 24 * 60 },
          { method: 'popup', minutes: 10 }
        ]
      }
  return event;
    };

    //console.log(event)

    var calendar = google.calendar('v3');

    calendar.events.insert(
    {
        auth: oauth2Client,
        calendarId: 'primary',
        resource: addEvents()
    },
    function(err, event) {
        if (err) {
          console.log(
            'There was an error contacting the Calendar 
             service: ' + err
          );
          return;
        }
        console.log('Event created: %s', event.htmlLink);
      }
    );
  }
});

暂无
暂无

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

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