nithinmurali / pygsheets

Google Sheets Python API v4
https://pygsheets.readthedocs.io/en/latest
Other
1.51k stars 222 forks source link

Error when authorizing: JSON Invalid file format #223

Closed whatareyoutalkingaboutyousoundinsane closed 6 years ago

whatareyoutalkingaboutyousoundinsane commented 6 years ago

I have an error message when trying to authorize. My code is just the following so far:

import pygsheets
gc = pygsheets.authorize(outh_file='/Users/Myusername/Projects/Python projects/google sheets test/client_secret.json') #full path to renamed .json file

Which returns the error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/oauth2client/clientsecrets.py", line 86, in _validate_clientsecrets
    (client_type, client_info), = clientsecrets_dict.items()
ValueError: too many values to unpack (expected 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Myusername/Projects/Python projects/google sheets test/test that baby action.py", line 3, in <module>
    gc = pygsheets.authorize(outh_file='/Users/Myusername/Projects/Python projects/google sheets test/client_secret.json')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pygsheets/client.py", line 596, in authorize
    outh_nonlocal=outh_nonlocal)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pygsheets/client.py", line 555, in get_outh_credentials
    flow = client.flow_from_clientsecrets(client_secret_file, SCOPES)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/oauth2client/client.py", line 2135, in flow_from_clientsecrets
    cache=cache)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/oauth2client/clientsecrets.py", line 165, in loadfile
    return _loadfile(filename)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/oauth2client/clientsecrets.py", line 126, in _loadfile
    return _validate_clientsecrets(obj)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/oauth2client/clientsecrets.py", line 89, in _validate_clientsecrets
    _INVALID_FILE_FORMAT_MSG + ' '
oauth2client.clientsecrets.InvalidClientSecretsError: Invalid file format. See https://developers.google.com/api-client-library/python/guide/aaa_client_secrets Expected a JSON object with a single property for a "web" or "installed" application

And, here's the json in case you were wondering:

{
  "type": "service_account",
  "project_id": "trusty-cacao-201620",
  "private_key_id": "ada3…3e4",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEv…aeny+rw\n-----END PRIVATE KEY-----\n",
  "client_email": "myname@trusty-cacao-201620.iam.gserviceaccount.com",
  "client_id": "109696671903923437286",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/myname%40trusty-cacao-201620.iam.gserviceaccount.com"
}

To be clear,

One thing I will say about the json file… On some other tutorial I read, their json file examples weren't formatted like mine—they had "web": or "installed": at the top of them.

Help? I feel like this could be user error as I'm very new to Python and thus this library as well… But at the same time, I am following the documentation's directions precisely. Or, could this be a problem with oauth2client…?

Thanks! Will appreciate any help.

Kordishal commented 6 years ago

Hi.

This is a service account file. You will need to pass it as authorize(service_file=...).