Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Google API access_token in Google Colab from Python

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!

like image 790
Jesús Arroyo Torrens Avatar asked Nov 07 '22 09:11

Jesús Arroyo Torrens


1 Answers

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.

like image 191
Nebulastic Avatar answered Nov 16 '22 12:11

Nebulastic