具有2个身份验证客户端会导致404创建日历事件

时间:2019-04-16 20:36:04

标签: node.js google-api google-api-nodejs-client

我正在尝试通过单个功能使用Google Node.js API客户端库在2个日历中创建2个日历事件。我正在使用2个单独的身份验证对象,如下所示:

var auth1 = await google.auth.getClient({ credentials: config.account1Creds, scopes: ["https://www.googleapis.com/auth/calendar.events"] }); 
var auth2 = await google.auth.getClient({ credentials: config.account2Creds, scopes: ["https://www.googleapis.com/auth/calendar.events"] });

我可以在第一个日历上创建活动,但是当我在第二个日历上创建活动时,我会从Google Calendar API服务器获得404 Not Found。

如果我注释掉第一行var auth1 = await google.auth...并仅在第二个日历上创建活动,那么一切都很好,并且活动创建成功。

有点像是第一次调用getClient,它设置了一个全局auth对象,该对象将用于所有剩余的API请求,并且无法替换,但这只是我和我的一种预感其实不知道。

任何人都知道为什么会这样吗?

编辑:

GetGoogleCalendarService: async function(credentials)
{
    var auth = await google.auth.getClient({ credentials: credentials, scopes: ["https://www.googleapis.com/auth/calendar.events"] });

    return google.calendar({ version: "v3", auth: auth });
},

InsertCalendarEvent: function(calendar, entry, callback)
{
    calendar.events.insert(entry, callback);
},

SendInvitesToEvent: async function (request, response)
{
    //build the calendar event
    var entry = {
        ...
    }

    //insert into operations calendar
    var opsCal = await Events.GetGoogleCalendarService(config.GetConfig().OpsCalendarCredentials);
    Events.InsertCalendarEvent(mainCal, entry);

    //insert into public calendar
    var publicCal = await Events.GetGoogleCalendarService(config.GetConfig().PublicCalendarCredentials);

    Events.InsertCalendarEvent(publicCal, entry, async function(err, event) 
    {
        //err: 404 Not Found
        ...
    }

}

1 个答案:

答案 0 :(得分:0)

  • You want to insert events to 2 calendars using 2 clients.
    • Client "A" inserts an event to Calendar "A".
    • Client "B" inserts an event to Calendar "B".
  • You are using googleapis of Node.js.

If my understanding is correct, how about this modification? In this modification, I separated retrieving auth by each credential.

Modified script:

const { google } = require("googleapis");

function insertEvent(calendar, calendarId) {
    // insert event
}

async function getService(c) {
  var auth = await google.auth.getClient({
    credentials: c,
    scopes: ["https://www.googleapis.com/auth/calendar.events"]
  });
  return google.calendar({ version: "v3", auth: auth });
}

function main() {
  getService(config.account1Creds).then(calendar => {
    insertEvent(calendar, "### calendarId ###");
  });
  getService(config.account2Creds).then(calendar => {
    insertEvent(calendar, "### calendarId ###");
  });
}

main();

Note:

  • This is a sample script. So please modify this for your situation.

In my environment, I could confirm that this script works. But if this didn't work and this modification was not the result you want, I apologize.

Edit:

From your current script, I modified as follows.

  • Is opsCal mainCal? In my modification, opsCal is used as mainCal.

Modified script:

From:
//insert into operations calendar
var opsCal = await Events.GetGoogleCalendarService(config.GetConfig().OpsCalendarCredentials);
Events.InsertCalendarEvent(mainCal, entry);

//insert into public calendar
var publicCal = await Events.GetGoogleCalendarService(config.GetConfig().PublicCalendarCredentials);

Events.InsertCalendarEvent(publicCal, entry, async function(err, event) 
{
    //err: 404 Not Found
    ...
}
To:
Events.GetGoogleCalendarService(config.GetConfig().OpsCalendarCredentials).then(
  opsCal => {
    Events.InsertCalendarEvent(opsCal, entry);
  }
);
Events.GetGoogleCalendarService(config.GetConfig().PublicCalendarCredentials).then(
  publicCal => {
    Events.InsertCalendarEvent(publicCal, entry);
  }
);