Can a template literal be utilized in the request body to perform a freebusy query on the Google Calendar API?

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

Need to verify calendar availability using the freebusy query

The date format required is: YYYY-MM-DDTHH:MM:SS.MMMZ . To achieve this, I extracted the input values and constructed a string literal. When logging it:

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

The console shows the date in the correct format (e.g. "2021-03-31T18:29:00.000Z"). However, when making the request, a 400 bad request error occurs. I suspect the issue lies in the string literal formatting, as the timeMin and timeMax values should not be enclosed in backticks `.`

``
I also attempted storing the date in a variable, but it was unsuccessful. Any advice on resolving this issue?

Answer №1

Through experimenting with Apps Script, I managed to recreate the issue you mentioned. Since Apps Script also utilizes Javascript, it confirmed that your observations were accurate. When utilizing template literals, the request parameters timeMin and timeMax end up being rendered as strings instead of in a datetime format. My solution involved concatenating the strings using the (+) operator, which proved to be successful.

Example Code:

  var calendarId = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddaebcb0adb1b89dbaafb2a8adf3bebcb1b8b3b9bcaff3bab2b2bab1b8f3beb2b0">[email protected]</a>";
  var date = {
    value: "2021-04-03"
  }
  var startTime = {
    value: "01:00"
  }
  var endTime = {
    value: "05:00"
  }
  
 // Incorrect 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);

  // Correct 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);

Result:

6:38:55 AM  Notice  Execution started
6:38:56 AM  Info    {items=[{<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e9808dd49a888499858ca98e9b869c99c78a88858c878d889bc78e86868e858cc78a8684">[email protected]</a>}], timeMax="2021-04-03T05:00:00.000Z", timeMin="2021-04-03T01:00:00.000Z", timeZone=GMT+08:00}
6:38:56 AM  Info    {items=[{<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7aea3fab4a6aab7aba287a0b5a8b2b7e9a4a6aba2a9a3a6b5e9a0a8a8a0aba2e9a4a8aa">[email protected]</a>}], 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={<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb988a869b878eab8c99849e9bc5888a878e858f8a99c58c84848c878ec5888486">[email protected]</a>={busy=[]}}, kind=calendar#freeBusy, timeMin=2021-04-03T01:00:00.000Z}
6:38:57 AM  Notice  Execution completed
  • It's noticeable that timeMin and timeMax are displayed as strings in the resource1 variable, while in resource2, they are in a non-string format.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Retrieve JSON Object Using Dynamically Generated String in Angular

Creating a controller in Angular involves retrieving URL parts and parameters using $statePrams. The $http service is then called to retrieve data from a JSON file. Once the data is received, the content of a specific object element - determined by $state ...

Turn a text file containing duplicate key value pairs into JSON format

I am trying to convert a file with the following format into JSON using either bash, jq, or python: Cus Id: 1234 Cus Name: 10:/John Parks Cus Type: temporary Cus client: tesla;toyota Dept Id: 111 Cus Id: 1235 Cus Name: 10:/William Parks Cus Type: temporar ...

Utilizing Electron API within an Angular Component

I'm hoping to utilize a locally stored video file in electron and play it within my angular component. Despite exposing the loadLocalFile function to prevent the need for setting nodeIntegration to true, I keep receiving a Security Warning : This re ...

Warning: The core schema has detected an unknown property `color` for the component or system `undefined` in Aframe + Vuejs. This issue was flagged within 10 milliseconds in

I am facing some challenges trying to integrate Aframe and vuejs seamlessly, as the console is displaying warning messages. It seems like Aframe is validating the attribute values before vue has a chance to modify them. Warning messages core:schema:warn ...

The Cascading of Bootstrap Card Designs

Looking for some assistance with my TV Show Searcher project that is based on an API. The functionality is complete, but I'm struggling to get the Bootstrap cards to stack neatly without any empty space between them. I want it to resemble the image ga ...

The best approach to incorporating interactive animation in next.js

My vision is to develop a character creation application using next js. The app should empower users to customize the character using sliders and gender selection buttons. The ultimate goal is to have a 2D animated version of the character that dynamicall ...

The React material-table only updates and rerenders the table when the data is updated twice

Currently, I am utilizing a tool called material-table (check it out here: https://material-table.com/#/) which has been developed using React. The issue I am facing is that the data passed as a prop to material-table doesn't seem to update correctly ...

What could be causing this issue of the Angular Material table not displaying properly?

I have been developing a Contacts application using Angular 9 and Angular Material. The goal is to display contact information and an image for each contact in a table row within the list component. Within my app.module.ts, I have imported various modules ...

Using Tween animations with Three.js

I have some queries regarding tween js within three.js. Below is the code snippet where the particles are generated as shown in the image: Image 1 // Code snippet for initializing var scene; var renderer; var container; var stats; var sphere; // Omitted ...

Running into an issue while attempting to generate functions in nodejs

I'm currently working on a function to authenticate a URL for a fetch request. However, when I attempt to call this function within the app.post callback, my Node.js server throws an error: "TypeError: authenticateUrl(...) is not a function". Does Nod ...

Convert the contents of the uploaded file to a JSON format

I've recently started using angularjs for file uploads and came across this helpful model on github: https://github.com/danialfarid/angular-file-upload The file upload process is working smoothly for me. However, I'm facing a challenge after upl ...

What steps can be taken to halt an ongoing AJAX call and initiate a new one with different parameters?

I recently implemented an ajax call using the jQuery.everyTime() function. The setup involves a combo box where users can select different graph names, triggering dynamic calls to an ajax function that returns JSON data and populates a chart in the View ev ...

Encountering Err_Connection_Refused while working with MVC WebAPI 2 and AngularJS

Seeking guidance on WebAPI, AngularJS, and .NET Authentication, I am currently following a tutorial mentioned HERE. The tutorial is brief (~under 10 mins), but I encountered an error stating Failed to load resource: net::ERR_CONNECTION_REFUSED. Typically, ...

I encountered an error while setting up Vue.js on my computer

While attempting to install Vue.js on my system using the command npm i -g @vue/cli, I encountered the following error: npm WARN cleanup Failed to remove some directories [ npm WARN cleanup [ npm WARN cleanup 'C:\\Users\\ ...

Can you provide instructions on creating a c# class for the following json object?

As a beginner in json, I have the following json object and need assistance in creating a C# class for it: { "JBS" : { "name" : "Jobsite" }, "LNK" : { "name" : "Linked IN" }, "MUK" : { "name" : "Monster UK" } } I requi ...

Vue Dev Tools is not updating data in Ionic Vue 3

There seems to be an issue with the updating of Reactive and Ref data in Vue Dev Tools when changes are made in an input field bound with v-model within a form. The updated data is only visible when I interact with another component and then return to the ...

Obtain the JSON data from the body of the current post request in Express.js

I have been working on retrieving the actual request body JSON in express.js but haven't been successful so far. Most of the resources I found online only show how to access the body of type ReqBody, whereas I am looking for a way to retrieve the actu ...

Angular Material selectionChanged function is providing the previous step instead of the current step

I need to make a page with two columns: one for a vertical stepper and the other for step descriptions. I want the description to update based on the current step selected. However, I am running into an issue where the selectedIndex shows the previously ch ...

Trouble with Firebase/Firestore documentation queries in JavaScript

Having trouble using the new Firestore system. I'm trying to retrieve a Collection of all users and go through it, but I can't seem to make it work. db.collection("users").get().then(function(querySnapshot){ console.log(querySnapshot.dat ...

Obtain log records in Azure and save them as a JSON object

Is there a recommended method to export logs from Azure in JSON format and what are the best practices for doing so? While using direct Kusto queries to export logs to CSV is an option, unfortunately there is no built-in option for exporting logs in JSON ...