# Error when using Google Calendar v3 API

**URL:** <https://discourse.nodered.org/t/error-when-using-google-calendar-v3-api/33054>\
**Category:** General\
**Created:** [18 September 2020 19:13 UTC](https://discourse.nodered.org/t/error-when-using-google-calendar-v3-api/33054 "2020-09-18T19:13:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phreaq](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@phreaq](https://discourse.nodered.org/u/phreaq)\
**Post date:** [18 September 2020 19:13 UTC](https://discourse.nodered.org/t/error-when-using-google-calendar-v3-api/33054/1 "2020-09-18T19:13:33Z")

</div>

I'm getting a "Error: Bad Request" error when checking the Google Calendar API.

I have a working setup, and a broken one, although I don't know where the issue is.

My working setup pulls the next 2 events:

```auto
msg.payload = { 
    "calendarId": "---@gmail.com", 
    "timeMin": new Date().toISOString(),
    "maxResults": "2",
    "orderBy": "startTime",
    "singleEvents": "true"
}
return msg;

```

whereas my broken code is trying to pull the next events, for tomorrow, not today.

```auto
var tomorrow = new Date(new Date().toISOString());
tomorrow.setDate(new Date().getDate()+1);
console.log(new Date(tomorrow.setHours(0,0,0,0)));

msg.payload = { 
    "calendarId": "---@gmail.com", 
    "timeMin": tomorrow,
    "maxResults": "2",
    "orderBy": "startTime",
    "singleEvents": "true"
}
return msg;

```

as far as I can see, the messages are the same (aside from the date/time)

this one works

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4203da85c647fe00221a8110eecf4c94973dd45.png)

this one doesn't

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/3208e74843389cb15cd0e8f1ec6ce2c5cdf5bc55.png)

any advice on how to fix it?

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [19 September 2020 00:19 UTC](https://discourse.nodered.org/t/error-when-using-google-calendar-v3-api/33054/2 "2020-09-19T00:19:24Z")

</div>

My guess would be that the debug output converts the date to a similar looking output but in the second it's still a Date object and not a string. On the first one `.toISOString()` is called but not on second one. So I would suggest trying to change it to have `tomorrow.toISOString()`. Might be something else of course but that's the only difference I can spot.

---

<div class="post-metadata">

**Author:** ![phreaq](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@phreaq](https://discourse.nodered.org/u/phreaq)\
**Post date:** [21 September 2020 14:08 UTC](https://discourse.nodered.org/t/error-when-using-google-calendar-v3-api/33054/3 "2020-09-21T14:08:53Z")

</div>

I tried the code below, and it still doesn't work.

```auto
var tomorrow = new Date();
tomorrow.setDate(new Date().getDate()+1);
console.log(new Date(tomorrow.setHours(0,0,0,0)));
tomorrow.toISOString();

```

I'll keep playing around (aka fighting with it)

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [22 September 2020 04:35 UTC](https://discourse.nodered.org/t/error-when-using-google-calendar-v3-api/33054/4 "2020-09-22T04:35:17Z")

</div>

How about:

```auto
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0,0,0,0);

msg.payload = { 
    calendarId: "---@gmail.com", 
    timeMin: tomorrow.toISOString(), 
    maxResults: "2",
    orderBy: "startTime",
    singleEvents: "true"
};

return msg;

```

This is based on the first Google hit for "javascript date tomorrow": [https://flaviocopes.com/how-to-get-tomorrow-date-javascript/](https://flaviocopes.com/how-to-get-tomorrow-date-javascript/)

---

<div class="post-metadata">

**Author:** ![phreaq](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@phreaq](https://discourse.nodered.org/u/phreaq)\
**Post date:** [29 September 2020 19:06 UTC](https://discourse.nodered.org/t/error-when-using-google-calendar-v3-api/33054/5 "2020-09-29T19:06:42Z")

</div>

thanks, that gave me what I needed (with a slight change to give timeMax too)

```auto
const today = new Date();
const tomorrow = new Date(today);
const nextday = new Date(today);

tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0,0,0,0);

nextday.setDate(nextday.getDate() + 2);
nextday.setHours(0,0,0,0);

msg.payload = { 
    calendarId: "---@gmail.com", 
    timeMin: tomorrow.toISOString(), 
    timeMax: nextday.toISOString(),
    orderBy: "startTime",
    singleEvents: "true"
};

return msg;

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 November 2020 19:06 UTC](https://discourse.nodered.org/t/error-when-using-google-calendar-v3-api/33054/6 "2020-11-28T19:06:50Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
