Daniyal dehleh
Daniyal dehleh

Reputation: 2382

Google Calendar: "Request had insufficient authentication scopes." when moving to calendar.readonly

Initially, I had the following scope:

SCOPES = ['https://www.googleapis.com/auth/calendar']

with the code:

credentials = google.oauth2.credentials.Credentials(
    **flask.session['credentials'])
service = build('calendar', 'v3', credentials=credentials)
events_result =service.calendarList().list().execute()
events = events_result.get('items', [])

which worked well. However, as I don't require access to write/delete user calendar & to prevent user from getting

See, edit, share and permanently delete all the calendars that you can access using Google Calendar

& to protect their safety, I narrowed my scope down to

SCOPES = ['https://www.googleapis.com/auth/calendar.events.readonly']

with the same code. However, now I am getting:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json returned "Request had insufficient authentication scopes.". Details: "Request had insufficient authentication scopes.">

Even though the doc mentions you're able to execute such code to retrieve events with both of the scopes.

Upvotes: 2

Views: 1746

Answers (1)

Tanaike
Tanaike

Reputation: 201388

Modification points:

  • When I saw your script and scope, you are trying to retrieve the calendar list from events_result =service.calendarList().list().execute() with the scope of https://www.googleapis.com/auth/calendar.events.readonly. I thought that this might be the reason of your issue.

Modified script:

When you want to retrieve the calendar list using your current script, please modify the scope to https://www.googleapis.com/auth/calendar.readonly.

When you want to retrieve the event list, please modify your script as follows. In this case, the scope of https://www.googleapis.com/auth/calendar.events.readonly can be used.

service = build('calendar', 'v3', credentials=creds)
events_result = service.events().list(calendarId='primary').execute()
events = events_result.get('items', [])
  • Please modify the calendar ID for your situation.

References:

Upvotes: 1

Related Questions