gpt4 book ai didi

python - Google API 构建服务对象返回 KeyError : 'rootUrl'

转载 作者:行者123 更新时间:2023-11-28 18:33:48 25 4
gpt4 key购买 nike

我有一个 Flask 应用程序,我可以在其中运行一个脚本(在 Flask 脚本的帮助下),该脚本使用以下代码使用 google api 发现:

应用脚本.py

import argparse
import csv
import httplib2

from apiclient import discovery
from oauth2client import client
from oauth2client.file import Storage
from oauth2client import tools


def get_auth_credentials():
flow = client.flow_from_clientsecrets(
'/path/to/client_screts.json', # file downloaded from Google Developers Console
scope='https://www.googleapis.com/auth/webmasters.readonly',
redirect_uri='urn:ietf:wg:oauth:2.0:oob')

storage = Storage('/path/to/storage_file.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
parser = argparse.ArgumentParser(parents=[tools.argparser])
flags = parser.parse_args(['--noauth_local_webserver'])
credentials = tools.run_flow(flow=flow, storage=storage, flags=flags)

return credentials

def main():
credentials = get_auth_credentials()
http_auth = credentials.authorize(httplib2.Http())
# build the service object
service = discovery.build('webmasters', 'v3', http_auth)

现在的问题是每次我在启动并再次运行脚本时关闭我的计算机,我在尝试构建服务对象时收到以下错误:

终端:

$ python app.py runscript
No handlers could be found for logger "oauth2client.util"
Traceback (most recent call last):
File "app.py", line 5, in <module>
testapp.manager.run()
File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
res = handle(*args, **config)
File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "/home/user/development/testproject/testapp/__init__.py", line 16, in runscript
metrics_collector.main()
File "/home/user/development/testproject/testapp/metrics_collector.py", line 177, in main
service = discovery.build('webmasters', 'v3', http_auth)
File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/oauth2client/util.py", line 140, in positional_wrapper
return wrapped(*args, **kwargs)
File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 206, in build
credentials=credentials)
File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/oauth2client/util.py", line 140, in positional_wrapper
return wrapped(*args, **kwargs)
File "/home/user/.virtualenvs/testproject/local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 306, in build_from_document
base = urljoin(service['rootUrl'], service['servicePath'])
KeyError: 'rootUrl'

已安装:

google-api-python-client==1.4.2
httplib2==0.9.2
Flask==0.10.1
Flask-Script==2.0.5

脚本有时运行*,但这就是问题我不知道为什么它有时运行而其他人不运行

*我试图让它工作的是,删除所有 cookie,再次从 Google Developers Console 下载 client_secrets.json,删除 storage_file.dat,从项目中删除所有 .pyc 文件

谁能帮我看看发生了什么?

最佳答案

来自一点研究 here ,似乎 No handlers could be found for logger "oauth2client.util" 错误实际上可能掩盖了一个不同的错误。您需要使用 logging模块并配置您的系统以输出。

解决方案

只需添加以下内容来配置日志记录:

import logging
logging.basicConfig()

其他有用/相关的帖子

Python - No handlers could be found for logger "OpenGL.error"

SOLVED: Error trying to access "google drive" with python (google quickstart.py source code)

关于python - Google API 构建服务对象返回 KeyError : 'rootUrl' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34291390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com