【发布时间】:2018-12-29 20:55:10
【问题描述】:
我启用了 Gmail API 并下载了包含令牌的 .json 文件。我将它放在与脚本相同的文件夹中。当我尝试运行它时,我得到了这个错误:
Traceback (most recent call last):
File "email_clean.py", line 14, in <module>
creds = store.get()
File "C:\Python27\lib\site-packages\oauth2client\client.py", line 407, in get
return self.locked_get()
File "C:\Python27\lib\site-packages\oauth2client\file.py", line 54, in locked_get
credentials = client.Credentials.new_from_json(content)
File "C:\Python27\lib\site-packages\oauth2client\client.py", line 302, in new_from_json
module_name = data['_module']
KeyError: '_module'
我知道对于同一问题还有其他几个关于 SO 的问题,但这些解决方案对我没有帮助。 token.json 文件与脚本位于同一文件夹中,据我所知,URL 似乎拼写正确。 JSON 文件看起来格式正确。任何帮助将不胜感激。
这是脚本:
"""
Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
# Setup the Gmail API
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))
# Call the Gmail API
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
if not labels:
print('No labels found.')
else:
print('Labels:')
for label in labels:
print(label['name'])
【问题讨论】:
标签: python oauth-2.0 gmail-api