Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to authenticate gspread with the default service account?

If I want to create/read/update spreadsheets using gspread, I know first have to authenticate like so:

import gspread

gc = gspread.service_account()

Where I can also specify the filename to point to a service account json key, but is there a way I can tell gspread to use the default service account credentials without pointing to a json?

My use-case is that I want to run gspread in a vm (or cloud function) that already comes with an IAM role and I can't seem to figure out where to get the json file from. I also don't want to copy the json to the vm unnecessarily.

like image 311
niczky12 Avatar asked Oct 25 '25 14:10

niczky12


1 Answers

You can use google.auth to get the credentials of the default service account. Then you can use gspread.authorize() with these credentials:

import google.auth
import gspread

credentials, project_id = google.auth.default(
    scopes=[
        'https://spreadsheets.google.com/feeds',
        'https://www.googleapis.com/auth/drive'
    ]
)
gc = gspread.authorize(credentials)
like image 85
tector Avatar answered Oct 27 '25 02:10

tector



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!