[英]Gmail API quickstart.py script returns KeyError '_module'
I enabled the Gmail API and downloaded the .json file containing the token. 我启用了Gmail API,并下载了包含令牌的.json文件。 I placed it in the same folder as the script.
我将其放置在脚本所在的文件夹中。 When I try to run it, I get this error:
当我尝试运行它时,出现以下错误:
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'
I am aware that there are several other questions on SO for this same issue, but those solutions did not help me. 我知道针对同一问题在SO上还有其他几个问题,但是这些解决方案对我没有帮助。 The token.json file is in the same folder as the script, and the URL appears to be spelled correctly as best I can tell.
token.json文件与脚本位于同一文件夹中,并且据我所知,URL似乎拼写正确。 The JSON file looks correctly formatted.
JSON文件的格式正确。 Any help would be greatly appreciated.
任何帮助将不胜感激。
Here is the script: 这是脚本:
"""
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'])
It's not finding / reading your token.json
file. 它不是在查找/读取您的
token.json
文件。 (You can look at that file and see it should have the first line read something like: (您可以查看该文件,并看到它的第一行内容如下:
{"_module": "oauth2client.client",
...
Simple fix (assuming your file has the correct info) is to pass the full absolute path name to file.Storage()
, rather than just the file name. 一个简单的解决方法(假设您的文件具有正确的信息)是将完整的绝对路径名传递给
file.Storage()
,而不只是文件名。
In your case, you say your json file starts with 就您而言,您说的json文件以
{"installed": {"client_id": ...
Thats's the your client secret json file, not the credentials json -- you'll need to use the former to get the latter. 那是您的客户端机密json文件,而不是凭据json-您需要使用前者来获取后者。 That file (with
"installed"
) is the one to be used with your call to client.flow_from_clientsecrets()
. 该文件(
"installed"
)是您对client.flow_from_clientsecrets()
调用使用的client.flow_from_clientsecrets()
。 On success, that will write the file as indicated by file.Storage()
, and on subsequent attempts, the flow will look in the token file and use information in there (rather than clientsecrets). 成功后,将如
file.Storage()
所示写入文件,并在随后的尝试中,流将在令牌文件中查找并在其中使用信息(而不是clientsecrets)。
A fix may be as simple as making sure your token.json file does not exist on first attempt as then it will attempt execute the flow & re-create the file. 修复可能很简单,只要确保您的token.json文件在第一次尝试时不存在,就可以尝试执行流程并重新创建该文件。
A really good explanation of the flow is : Ashok Yogi's A beginners guide to Google OAuth and Google APIs 关于流程的一个很好的解释是: Ashok Yogi的Google OAuth和Google API入门指南
Figured it out. 弄清楚了。 The problem was that I had saved the .json file as token.json, not realizing that this is a file that gets created once it authenticates.
问题是我将.json文件另存为token.json,但没有意识到这是一个通过身份验证即创建的文件。 The trick was to delete token.json and then save the .json file from the Developer's Console as credentials.json.
技巧是删除token.json,然后从开发人员控制台将.json文件另存为凭据.json。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.