Search code examples
javascriptjsongoogle-calendar-apistring-literalsgoogle-apis-explorer

Is it possible to make a freebusy query to the Google Calendar API with a template literal in the request body?


var freeRequest = gapi.client.calendar.freebusy.query({
                "items": [
                    {"id": calendarid}
                ],
                "timeMin": `"${date.value}T${startTime.value}:00.000Z"`,
                "timeMax": `"${date.value}T${endTime.value}:00.000Z"`,
                "timeZone": "GMT+01:00",
            });

I want to check if a calendar is busy via the freebusy query

I know the request only works with the date format: YYYY-MM-DDTHH:MM:SS.MMMZ . My plan was to paste the value that I get from an html input into a string literal and then format it accordingly. When I console.log:

`"${date.value}T${endTime.value}:00.000Z"`

The console gives me the date in the right format (e.g. "2021-03-31T18:29:00.000Z"). But when sending the request in my application it gives me a 400 bad request error. I guess the problem lies in the string literal because the timeMin and timeMax values should only have quotation marks around it and not the

``
I also tried saving the date in a variable, but this also did not work. Is there a way to solve this?


Solution

  • I was able to replicate your issue using Apps Script since it also uses Javascript. Your findings are correct, when you use the template literals, the request parameter timeMin and timeMax becomes a string rather than a datetime format. What I do was to concatenate the string using (+) operator and it works.

    Sample Code:

      var calendarId = "[email protected]";
      var date = {
        value: "2021-04-03"
      }
      var startTime = {
        value: "01:00"
      }
      var endTime = {
        value: "05:00"
      }
      
     // Bad request
      var resource1 = {
        timeMin: `"${date.value}T${startTime.value}:00.000Z"`,
        timeMax: `"${date.value}T${endTime.value}:00.000Z"`,
        items: [
          {
            id: calendarId
          }
        ],
        timeZone: "GMT+08:00"
      };
    
      Logger.log(resource1);
    
      // Successful request
      var resource2 = {
        timeMin:  date.value+"T"+startTime.value+":00.000Z",
        timeMax: date.value+"T"+endTime.value+":00.000Z",
        items: [
          {
            id: calendarId
          }
        ],
        timeZone: "GMT+08:00"
      };
      Logger.log(resource2);
      var request = Calendar.Freebusy.query(resource2);
      Logger.log(request);
    

    Output:

    6:38:55 AM  Notice  Execution started
    6:38:56 AM  Info    {items=[{[email protected]}], timeMax="2021-04-03T05:00:00.000Z", timeMin="2021-04-03T01:00:00.000Z", timeZone=GMT+08:00}
    6:38:56 AM  Info    {items=[{[email protected]}], timeMax=2021-04-03T05:00:00.000Z, timeMin=2021-04-03T01:00:00.000Z, timeZone=GMT+08:00}
    6:38:56 AM  Info    {timeMax=2021-04-03T05:00:00.000Z, calendars={[email protected]={busy=[]}}, kind=calendar#freeBusy, timeMin=2021-04-03T01:00:00.000Z}
    6:38:57 AM  Notice  Execution completed
    
    • Notice that the timeMin and timeMax becomes a string in the resource1 variable while in resource2 they are not in string format