I am currently using google sheets and is using the python library gspread to automate it. The first lines of my code would be to import the gspread library and login to a service account using the service-account.json file google console gives us.
import gspread account = gspread.service_account(filename='service-account.json') but i want it to take a python dictionary instead of a file, so i wantsomething like this
import gspread creds = {'type': 'service_account', 'project_id': 'hidden', 'private_key_id': 'hidden', 'private_key': 'hidden', 'client_email': 'hidden', 'client_id': 'hidden', 'auth_uri': 'hidden', 'token_uri': 'hidden', 'auth_provider_x509_cert_url': 'hidden', 'client_x509_cert_url': 'hidden'} #login to service account using these creditionals i cant seem to find anything that does this, maybe due to my lack of searching skill. thanks
1 Answer
When I saw the official document of gspread, I found the following sample script. Ref
Sample script:
import gspread credentials = { "type": "service_account", "project_id": "api-project-XXX", "private_key_id": "2cd … ba4", "private_key": "-----BEGIN PRIVATE KEY-----\nNrDyLw … jINQh/9\n-----END PRIVATE KEY-----\n", "client_email": "[email protected]", "client_id": "473 … hd.apps.googleusercontent.com", ... } gc = gspread.service_account_from_dict(credentials) sh = gc.open("Example spreadsheet") print(sh.sheet1.get('A1')) When I tested this script,
token_uriwas required to be included as follows. And, the values ofprivate_key,client_emailandtoken_uriwere required to be used in my situation. But, I'm not sure whether my situation is the same with your situation. So, please test the above script in your situation.credentials = { "private_key": "-----BEGIN PRIVATE KEY-----\nNrDyLw … jINQh/9\n-----END PRIVATE KEY-----\n", "client_email": "[email protected]", "token_uri": "", }