6

I need to generate an access_token with some specific scopes ['https://www.googleapis.com/auth/bigquery', 'https://www.googleapis.com/auth/cloud-platform'] from Python inside Google Colab.

By default, Google Colab provides a function inside colabtools to authenticate the user.

from google.colab import auth
auth.authenticate_user()
print('Authenticated')

But the credential file generated contains a list of fixed scopes, not including the ones I need.

import os
with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) as f:
  print(f.read())
...
"scopes": [
    "https://www.googleapis.com/auth/drive",
    "https://www.googleapis.com/auth/appengine.admin",
    "https://www.googleapis.com/auth/accounts.reauth",
    "https://www.googleapis.com/auth/cloud-platform",
    "https://www.googleapis.com/auth/compute",
    "https://www.googleapis.com/auth/userinfo.email"
  ],
...

I have tried to generate a valid Credentials token with the google-auth library enter code hereusing the google.auth.Credentials class and also the google.oauth2.Credentials class. I can generate valid instances of the classes, but when I check the token is None.

Is there a way to generate valid access_token for Google API with custom scopes from Python?

Thanks!

1 Answer 1

1

If you are using a VM with Google Colab, you can set the scopes for the service account on that vm with the following command (example):

gcloud compute instances set-service-account example-instance \
   --service-account [email protected] \
   --scopes compute-rw,storage-ro

Then, when authenticating from within Python, the generated credentials will contain the correct scopes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.