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

时间:2015-12-15 14:11:43

标签: python google-api google-oauth

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

app_script.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文件

任何人都可以帮我看看发生了什么吗?

2 个答案:

答案 0 :(得分:1)

从一点点研究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)

答案 1 :(得分:0)

非常感谢您提示Avantol13,您是对的,因为屏蔽了错误。

问题在于以下几行:

service = discovery.build('webmasters', 'v3', http_auth)

实际应该是:

service = discovery.build('webmasters', 'v3', http=http_auth)

现在全部工作。感谢