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 here
using 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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With