简体   繁体   English

Google API quickstart.py 错误 KeyError: '_module'

[英]Google API quickstart.py error KeyError: '_module'

Using Gmail API.使用 Gmail API。 My client secret file is downloaded and working for Ruby.我的客户端机密文件已下载并适用于 Ruby。 When I try the quickstart.py (python) version I get this error当我尝试 quickstart.py (python) 版本时,我收到此错误

File "quickstart.py", line 70, in <module>
    main()
  File "quickstart.py", line 55, in main
    credentials = get_credentials()
  File "quickstart.py", line 38, in get_credentials
    credentials = store.get()
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 374, in get
    return self.locked_get()
  File "/Library/Python/2.7/site-packages/oauth2client/file.py", line 79, in locked_get
    credentials = Credentials.new_from_json(content)
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 281, in new_from_json
    module = data['_module']
KeyError: '_module'

I have not changed the file at all, just added the client_secret.json to that working directory and also install the google-api-python-client.我根本没有更改文件,只是将 client_secret.json 添加到该工作目录并安装了 google-api-python-client。 My python code came from here: https://developers.google.com/gmail/api/quickstart/python我的 python 代码来自这里: https : //developers.google.com/gmail/api/quickstart/python

oauth2client is trying to load credentials from a json file with an incorrect structure. oauth2client 正在尝试从结构不正确的 json 文件加载凭据。

Maybe the Ruby client uses a different file format but I'd be surprised.也许 Ruby 客户端使用不同的文件格式,但我会感到惊讶。 Are you sure you didn't save client_secret.json as ~/.credentials/gmail-quickstart.json accidentally?您确定您没有将client_secret.json意外保存为~/.credentials/gmail-quickstart.json吗?

Regardless, removing ~/.credentials/gmail-quickstart.json and re-authenticating will generate a new credentials file with the correct structure.无论如何,删除~/.credentials/gmail-quickstart.json并重新进行身份验证将生成具有正确结构的新凭据文件。

Try replacing creds = store.get() with creds = None temporarily.尝试暂时用creds = None替换creds = store.get() If this works, you can refactor your code to always start with flow-based credentials instantiation.如果可行,您可以重构代码以始终从基于流的凭据实例化开始。 This worked for me.这对我有用。 It seems Google samples are out of sync with their oauth2client.似乎 Google 样本与其 oauth2client 不同步。

I am learning Python myself and had a similar problem, but with the Calendar API example .我自己正在学习 Python 并且遇到了类似的问题,但是使用Calendar API 示例 It turned out that it was a typo with regards to the SCOPE.事实证明,这是关于 SCOPE 的一个错字。

## Typo - Invalid definition
SCOPES = 'https://ww.googleapies.com/auth/calendar.readonly'
## Correct Value for SCOPE
SCOPES = 'https://www.googleapis.com/auth/calendar'

Also, Matt's answer help point me in the right direction.此外,马特的回答帮助我指明了正确的方向。 The gmail-quickstart.json is not the same thing as the client_secret.json. gmail-quickstart.json 与 client_secret.json 不同。 The client_secret.json allows you to make a request for an OAuth2 token. client_secret.json 允许您请求 OAuth2 令牌。 While the gmail-quickstart.json contains the issued token and meta-data associated with it.而 gmail-quickstart.json 包含发布的令牌和与之关联的元数据。 The gmail-quickstart.json isn't created until you successfully login.在您成功登录之前,不会创建 gmail-quickstart.json。

One last thought, in order to log in successfully, the quickstart.py app launched an instance of my web-browser (Firefox) and went to the Google login screen.最后一个想法,为了成功登录,quickstart.py 应用程序启动了我的网络浏览器 (Firefox) 的一个实例并转到了 Google 登录屏幕。 In order for Firefox to run properly, I had to set my DISPLAY variable properly first.为了让 Firefox 正常运行,我必须首先正确设置我的 DISPLAY 变量。

$ export DISPLAY=:0
$ xhost +
access control disabled, clients can connect from any host

I solved this by moving the client_secret.json to the same directory as the py file that is trying to read it (quickstart.py), mine was on the Desktop while i had saved the json to Documents.我通过将 client_secret.json 移动到与试图读取它的 py 文件 (quickstart.py) 相同的目录来解决这个问题,我的文件在桌面上,而我已将 json 保存到文档中。 I saved the json to the Desktop too and boy, It flew!.我也将 json 保存到桌面,男孩,它飞了!。

I dont know why it doesnt work when they are in different directories, defining a custom credential_path doesn't help.我不知道为什么当它们在不同的目录中时它不起作用,定义自定义 credential_path 没有帮助。

In this GitHub issue: error "KeyError: '_module'" when running gdrive_upload.py在此 GitHub 问题中:运行 gdrive_upload.py 时出现错误“KeyError: '_module'”

sputnik-dev answared on 10 Jan 2016: sputnik-dev于 2016 年 1 月 10 日回答:

If someone have the same issue : auth_token.txt and client_secret.json are not the same!如果有人有同样的问题: auth_token.txtclient_secret.json不一样! Don't link the client_secret.json from google API console.不要从 google API 控制台链接client_secret.json The file will be automatically created by the script.该文件将由脚本自动创建。

Wrong way: gauth.SaveCredentialsFile("client_secret.json")错误的方式: gauth.SaveCredentialsFile("client_secret.json")

Right way: gauth.SaveCredentialsFile("<any random name>.json")正确的方法: gauth.SaveCredentialsFile("<any random name>.json")

Extra:额外的:

PyDrive code that automate google drive api authetication. PyDrive 代码,可自动执行 google drive api 身份验证。 Use the browser just one time to authenticate and never more.仅使用浏览器一次进行身份验证,以后再也不使用了。 It saves your credential data on mycreds.json :)它将您的凭据数据保存在mycreds.json :)

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LoadCredentialsFile("mycreds.json")

if gauth.credentials is None:
    gauth.LocalWebserverAuth()

elif gauth.access_token_expired:
    gauth.Refresh()

else:
    gauth.Authorize()

gauth.SaveCredentialsFile("mycreds.json")

I followed several recommendations listed here:我遵循了此处列出的几项建议:

  • moving the client_secret.json to the same directory as the py file that is trying to read it, as recommended by simic0de.按照 simic0de 的建议,将 client_secret.json 移动到与尝试读取它的 py 文件相同的目录中。

  • Working with skirill idea, instead of completely eliminating the 'credentials=store.get()', I decided to handle the exception, so it would work with flow-based credentials instantiation.使用skirill 想法,而不是完全消除'credentials=store.get()',我决定处理异常,因此它可以与基于流的凭证实例化一起使用。

The final code is:最后的代码是:

try:
    credentials = store.get()
except:
    print('Working with flow-based credentials instantiation')

You can write your own code in the except line.您可以在 except 行中编写自己的代码。 This approach will allow the store.get() command to work when conditions are met.这种方法将允许 store.get() 命令在满足条件时工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gmail API quickstart.py脚本返回KeyError&#39;_module&#39; - Gmail API quickstart.py script returns KeyError '_module' Google Drive API Quickstart.py 错误 400:redirect_uri_mismatch - Google Drive API Quickstart.py Error 400: redirect_uri_mismatch 由于 JSONDecodeError:额外数据,Google 的 quickstart.py 未连接到 Google Workspace API - Google's quickstart.py not connecting to Google Workspace API because of JSONDecodeError: Extra data 尝试使用python访问“google drive”时出错(google quickstart.py源代码) - Error trying to access “google drive” with python (google quickstart.py source code) 适用于我的 Google Sheets 电子表格的 Python quickstart.py - Python quickstart.py adapted to my Google Sheets spreadsheet 由于硒问题,Quickstart.py无法运行 - Quickstart.py failed to run due to selenium issue Google API Python - KeyError: _module - Google API Python - KeyError: _module 适用于Python的Google API v4 KeyError:“ _ module” - Google API v4 for Python KeyError: '_module' Google API:调用get_credentials()时,表KeyError:“ _ module” - Google API: sheets KeyError: '_module' while calling get_credentials() 谷歌 forms api 快速入门 python - google forms api quickstart python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM