Quickstart.py'模块'KeyError错误。如何使该错误消失?

时间:2018-06-20 05:59:13

标签: python python-3.x google-api

我正在跟着Google快速入门文档,试图从Google工作表中简单地访问数据。我按照代码逐行跟踪。

到目前为止,这是我的代码:

    """
Shows basic usage of the Sheets API. Prints values from a Google Spreadsheet.
"""
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# Setup the Sheets API
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_ID.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))

# Call the Sheets API
SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
RANGE_NAME = 'Class Data!A2:E'
result = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID,
                                             range=RANGE_NAME).execute()
values = result.get('values', [])
if not values:
    print('No data found.')
else:
    print('Name, Major:')
    for row in values:
        # Print columns A and E, which correspond to indices 0 and 4.
        print('%s, %s' % (row[0], row[4]))

我将client_id.json文件和credential.json文件都放在了桌面下jupyter笔记本文件所在的目录中。这给了我一个“模块”键错误。

这是完整的错误:

KeyError                                  Traceback (most recent call last)
<ipython-input-1-3ecf3bea682b> in <module>()
     10 SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
     11 store = file.Storage('credentials.json')
---> 12 creds = store.get()
     13 if not creds or creds.invalid:
     14     flow = client.flow_from_clientsecrets('client_ID.json', SCOPES)

/anaconda3/lib/python3.6/site-packages/oauth2client/client.py in get(self)
    405         self.acquire_lock()
    406         try:
--> 407             return self.locked_get()
    408         finally:
    409             self.release_lock()

/anaconda3/lib/python3.6/site-packages/oauth2client/file.py in locked_get(self)
     52 
     53         try:
---> 54             credentials = client.Credentials.new_from_json(content)
     55             credentials.set_store(self)
     56         except ValueError:

/anaconda3/lib/python3.6/site-packages/oauth2client/client.py in new_from_json(cls, json_data)
    300         # Find and call the right classmethod from_json() to restore
    301         # the object.
--> 302         module_name = data['_module']
    303         try:
    304             module_obj = __import__(module_name)

KeyError: '_module'

我尝试过转换凭据=无,但没有成功,只是引发了另一个错误。我不确定出了什么问题,也不确定错误的逻辑。

先谢谢了。

0 个答案:

没有答案