简体   繁体   中英

Python - Reading a Google Spreadsheet fails with module_name = data['_module'] in line 302, in new_from_json

I have a Google Spreadsheet that I can access with my user and I want to read a range of it via Python 3.6 as a Task in Windows.

I found a few good tutorials on this sort of thing and so I constructed the following:

def get_credentials():
#    "Gets valid user credentials from storage."

    credential_dir = os.getcwd();
    credential_path = os.path.join(credential_dir, 'client_secret.json');
    print(credential_path);


    store = Storage(credential_path);
    credentials = store.get();

    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

credentials = get_credentials();

I have created a client_secret.json file in the working directory and here are the pseudo-contents (structure unchanged, but keys changed):

{"installed":{"client_id":"1002948203906-cpsdfyhbghjhtreetryy543f3q8m25kv.apps.googleusercontent.com","project_id":"MySolutionTst","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_secret":"ao2sDWEDTGVHWdddfuieXJqr","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}

I think that everything should work, but it fails with these errors:

C:\Users\MyUser\Documents\Visual Studio 2017\Projects\MySolution\MySolution\client_secret.json
Traceback (most recent call last):
  File "C:\Users\MyUser\Documents\Visual Studio 2017\Projects\MySolution\MySolution\MySolution.py", line 135, in <module>
    credentials = get_credentials();
  File "C:\Users\MyUser\Documents\Visual Studio 2017\Projects\MySolution\MySolution\MySolution.py", line 67, in get_credentials
    credentials = store.get();
  File "C:\Program Files\Python36\lib\site-packages\oauth2client\client.py", line 407, in get
    return self.locked_get()
  File "C:\Program Files\Python36\lib\site-packages\oauth2client\file.py", line54, in locked_get
    credentials = client.Credentials.new_from_json(content)
  File "C:\Program Files\Python36\lib\site-packages\oauth2client\client.py", line 302, in new_from_json
    module_name = data['_module']
KeyError: '_module'
Press any key to continue . . .

I am still a newbie in Python and definitely a newbie in Google Docs and I cannot see where it is going wrong. All I can imagine is that the structure of the client_secret.json is incorrect, but I downloaded the client secret json file from the Google API Manager and saved it straight from there (and have not made any changes).

Other than that, what has gone wrong?

Kindest regards,

Try not calling

credentials = store.get();

Just temporarily replace it with

credentials = None

That is, let your code reach the fallback method of retrieving credentials that starts with:

flow = client.flow_from_clientsecrets(...

In my case I've downloaded client_secret.json from my Google API account but its structure obviously does not match the one expected by new_from_json() . Probably in the past it was returning None but now it throws an exception and breaks this example. Once I've excluded this call I could authenticate this app in a browser and got its output in console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM