1

I'm trying to create a new event using the Google Calendar, I'm currently using their Javascript API. When I try to do the request, the responsive that I get is a 403 - forbidden. I tested the same request with the API Explorer available on the Google Developers page and it worked normally.

Searching for the problem on the internet, I found some solutions, where people said that I need to share my calendar with the Google Service Account I created. I did it and it still doesn't work. Do you guys have any idea how can I solve it?

EDIT

Initialization:

gapi.client.init({
            discoveryDocs: DISCOVERY_DOCS,
            clientId: CLIENT_ID,
            scope: SCOPES
        }).then(function () {
            if (!(gapi.auth2.getAuthInstance().isSignedIn.get())) {
                handleAuthClick();
            }
            else {
                getAvailableTimes();
            }
            // Listen for sign-in state changes.
            // gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
        });

Insert action:

modal.result.then(function (interview) {
            var eventRequest = gapi.client.calendar.events.insert({
                calendarId: interview.recruiter.email,
                sendNotifications: true,
                resource: {
                    summary: 'Interview with ' + interview.candidate.name,
                    description: '',
                    start: {
                        dateTime: interview.start
                    },
                    end: {
                        dateTime: interview.end
                    },
                    attendees: [
                        {email: interview.recruiter.email},
                        {email: interview.candidate.email}
                    ],
                    reminders: {
                        useDefault: false,
                        overrides: [
                            {method: 'email', minutes: 60 * 24},
                            {method: 'email', minutes: 60},
                            {method: 'popup', minutes: 60},
                            {method: 'email', minutes: 10},
                            {method: 'popup', minutes: 10}
                        ]
                    }
                }
            });
        })

2 Answers2

1

Service accounts are not your typical regular accounts. The sample in the Events.insert page you mentioned doesn't use a service account. It uses your regular account. Service accounts needs further setting up like:

  1. Calendar API is enabled in the Google Dev Console
  2. Service Account is created
  3. Store the p12 key and store that in your project folder where you'll use it later.

You can also check this SO post for added reference and this official Service Accounts doc for Google APIs.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • I'm using JavaScript to access the Google API (GAPI). While using it, the only things it requested was the Scopes and the Client ID. For the Client ID I set one for a Web Application I created on the [Google Developer Console](https://console.developers.google.com). I also created the service account itself, but if I set it as the Client ID on the init config, the request will return a error saying that my address is not whitelisted. How can I proceed from here? – Arthur Mendonça Ribeiro Sep 25 '17 at 17:52
  • can you clarify, are you using a service account for this project? – ReyAnthonyRenacia Sep 26 '17 at 04:39
1

While also searching for a solution on the internet, I found what is cause of this problem exactly. You can't connect with a Service Account using the Javascript API, as I was using. :(