403 Forbidden message when calling the v3 Google Calendar API using a Service Account via OAuth 2.0

南楼画角 提交于 2019-12-17 07:38:24

问题


This is a follow on from my thread about a 401 error when using the Google Calendar API and OAuth2, which can be found here

This contains details of the account setup that leads onto my next question, so I wont repeat myself in this thread.

OK, so when when I call the following code to update a Calendar event I get a 403 forbidden error.

for (Event event : events.getItems())
{
    event.setSummary("XXX" + event.getSummary());

    Event updatedEvent = calendar.events().update(CALENDAR_ID, event.getId(), event).execute();
}

Here is the returned error message:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
    "code" : 403,
    "errors" : [ {
        "message" : "Forbidden"
     } ],
    "message" : "Forbidden"
}

What have I tried? Well, I have re-read the (rather poor) Google documentation about Service Accounts, re-checked my API Console account settings, tried changing the code that builds the credential (this leads to other errors so is a regression on my previous thread).

In short, nothing works, so is there anything obvious I am missing?


回答1:


Solved, thanks to this post.

You have to share the calendar from your Google Calendar account with the Service Account email that is generated in the Google API Console, e.g. 284XXXXXXXX@developer.gserviceaccount.com.

I can now update my calendar from my web service.




回答2:


I was also getting the same error even after sharing the calendar as in the Justin's answer. After comparing with another working sample figured out that I have set

var SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"];

instead of

var SCOPES = ["https://www.googleapis.com/auth/calendar"];

Hope this helps anyone who has done a similar mistake and getting this error.



来源:https://stackoverflow.com/questions/14003203/403-forbidden-message-when-calling-the-v3-google-calendar-api-using-a-service-ac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!