1

I've run:

gcloud auth application-default login --client-id-file google_oauth_client_id.json --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/calendar.calendars.readonly"

successfully. My browser opened, I granted the calendar and cloud-platform permissions to my test app, and the results were saved to disk:

Credentials saved to file:[/home/*****/.config/gcloud/application_default_credentials.json]

However, running the following snippet leads to a 403 error:

from google.auth import default
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
SCOPES = ["https://www.googleapis.com/auth/calendar.calendars.readonly"]
credentials, project_id = default(scopes=SCOPES, quota_project_id='my-project-id')
credentials.refresh(Request())
access_token = credentials.token
service = build("calendar", "v3", credentials=credentials)
events = service.events().list(calendarId="My Calendar Id", maxResults=10, singleEvents=True, orderBy="startTime").execute()

At first I thought maybe I wasn't using the correct calendarId, but when I was in the debugger, I noticed that the credentials object has no scopes defined:

>>> (credentials.scopes, credentials.default_scopes, credentials.granted_scopes)
(None, None, None)

However, if I delete the application_default_credentials.json file the default method throws an appropriate error, so it does seem like it's reading from the file properly-- it's just not realizing that the permissions have been granted...

Looking at the application_default_credentials.json, I'm not seeing any mention of scopes: dict_keys(['account', 'client_id', 'client_secret', 'refresh_token', 'type', 'universe_domain'])

This leads me to believe that either:

  1. The scopes are saved server-side, and I need to properly request them when refreshing the token
  2. The gcode client isn't saving this information properly.

Option 1 seems more likely, since the CLI is properly displaying the scopes and passing them to the OAuth session....

asked Sep 4 at 21:22
4
  • have you tried adding https://www.googleapis.com/auth/cloud-platform into the SCOPES? try changing your scope into SCOPES = ["https://www.googleapis.com/auth/cloud-platform","https://www.googleapis.com/auth/calendar.calendars.readonly"] Commented Sep 4 at 21:46
  • Good shout, but no, the scopes properties are still all None Commented Sep 5 at 9:47
  • 2
    You want calendar.events.readonly not calendar.calendars.readonly for service.events().list. Your issue is that the scopes granted by gcloud auth application-defaullt login to your user credentials do not include this scope. You may verify e.g. https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={ACCESS_TOKEN} and can fix by adding the scope when you login e.g. gcloud auth application-default login --scopes=https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/calendar.events.readonly Commented Sep 5 at 14:49
  • @DazWilkin this fixed the problem, if you'd like to submit it as an answer I'll accept it so you get the karma :) Commented Sep 6 at 11:00

1 Answer 1

3

You should use calendar.events.readonly for service.events().list.

This is documented by APIs Explorer for Calendar API for events.list under Authorization

Your issue is that the scopes granted by gcloud auth application-default login to your user credentials do not include this scope. You may verify using e.g. this link:

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={ACCESS_TOKEN}

When you login, you can request the additional scopes:

gcloud auth application-default login \
--scopes=\
https://www.googleapis.com/auth/cloud-platform,\
https://www.googleapis.com/auth/calendar.events.readonly
answered Sep 6 at 15:49
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.