0
votes

I want to insert an event in one of my public calendars using the following code, But it returns error 401 login required

I am getting Request is missing required authentication credential. Expected OAuth 2 access token error while insert events. Where I am doing wrong ?

Here is my code

      var mykey = "MY API KEY";
      var calendarid = "MY CALENDAR ID LIKE [email protected]";
      var event = {
        summary: "Google I/O 2015",
        location: "800 Howard St., San Francisco, CA 94103",
        description: "Heyyyyyy",
        start: {
          dateTime: "2021-02-16T09:00:00-07:00",
          timeZone: "America/Los_Angeles",
        },
        end: {
          dateTime: "2021-02-17T17:00:00-07:00",
          timeZone: "America/Los_Angeles",
        },
        recurrence: ["RRULE:FREQ=DAILY;COUNT=2"],
        attendees: [
          { email: "[email protected]" },
          { email: "[email protected]" },
        ],
        reminders: {
          useDefault: false,
          overrides: [
            { method: "email", minutes: 24 * 60 },
            { method: "popup", minutes: 10 },
          ],
        },
      };

      $.ajax({
        type: "POST",
        url: encodeURI(
          "https://www.googleapis.com/calendar/v3/calendars/" +
            calendarid +
            "/events/?key=" +
            mykey
        ),
        dataType: "json",
        data: event,
        success: function (response) {
          //do whatever you want with each
        },
        error: function (response) {
          //tell that an error has occurred
        },
      });```

Get API is working fine, but I am getting above error with post API.

I do not know whY I am getting this error. Can anyone help me ? What is the issue ?

1

1 Answers

2
votes

If you check the documentation for events insert you will find that this method requires that you be authenticated with one of the following scopes

enter image description here

You appear to be missing the Oauth2 section. YOu cant just use an API key api keys only give you access to public data you need to use Oauth2 to authorize a user

 /**
       *  Initializes the API client library and sets up sign-in state
       *  listeners.
       */
      function initClient() {
        gapi.client.init({
          apiKey: API_KEY,
          clientId: CLIENT_ID,
          discoveryDocs: DISCOVERY_DOCS,
          scope: SCOPES
        }).then(function () {
          // Listen for sign-in state changes.
          gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);

          // Handle the initial sign-in state.
          updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
          authorizeButton.onclick = handleAuthClick;
          signoutButton.onclick = handleSignoutClick;
        }, function(error) {
          appendPre(JSON.stringify(error, null, 2));
        });
      }

I recommend looking into the Google calendar api quickstart for javascript.

Note: API keys only give you access to read even if the calendar is public you still need to use Oauth2 and authorize a user in order to write to the calendar.