DEV Community

Peter Jacxsens
Peter Jacxsens

Posted on • Updated on

using google-api-javascript-client (gapi) with no authentication [code example]

Here is an example of using Google APIs Client Library for browser JavaScript, aka gapi with no authentification. The Google Api we are calling here is Calendar API.

There are some caveats:

  • We will be calling a public calendar.
  • It's a read-only request.
  • You do need an api key from google cloud console.

First load api.js: <script src=""></script>. Then add the following script:

  // fill in your api key
  const apiKey = "******";
  // select a calendar by id
  const calendarId = "******";

  function start() {
    // Initializes the client with the API key and the Calendar API.
      'apiKey': apiKey,
      // load the correct library (calendar in this case)
      'discoveryDocs': [''],
      'scopes': ''
    }).then(function() {
        'calendarId': calendarId,
        'maxResults' : 5,
    }).then(function(response) {
    }, function(reason) {
      console.log('Error: ', reason);
  // Loads the JavaScript client library and invokes `start` afterwards.
  gapi.load('client', start);
Enter fullscreen mode Exit fullscreen mode

This example is based on a sample in the google-api-javascript-client:

Top comments (0)