【问题标题】:Python - HttpError when using google drive APIPython - 使用谷歌驱动 API 时出现 HttpError
【发布时间】:2015-05-01 05:36:56
【问题描述】:

我正在使用 python 3.4 来利用 Google API 从用户的谷歌驱动器访问和读取文件。如果用户在拥有凭据文件之前已经使用过该应用程序,那么我希望能够通过尝试列出用户驱动器上的文件来测试凭据是否仍然有效。想法是,如果出现此错误,则应用知道它需要再次请求访问权限。

经过大量搜索后,我尝试将以下示例中的代码拼凑起来:
Google API commands
Google Example

我目前有以下代码:

import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow

def getAccess():
    flow = OAuth2WebServerFlow(client_id, client_secret, scope,     redirect_uri="urn:ietf:wg:oauth:2.0:oob")
    auth_uri = flow.step1_get_authorize_url()
    print("Please go to the following webpage and copy and paste the access key onto the command line:\n" + auth_uri + "\n")
    code = input("Please enter your access code:\n")
    credentials = flow.step2_exchange(code)
    storage.put(credentials)

client_id = MYCLIENT_ID
client_secret = MYCLIENT_SECRET
scope = "https://www.googleapis.com/auth/drive"

storage = Storage('~/credentials.dat')
credentials = storage.get()

if credentials is None or credentials.invalid:
    getAccess()
else:
    try:
        http = httplib2.Http()
        http = credentials.authorize(http)
        service = build('drive', 'v2', http=http)
        param = {}
        service.files().list(**param).execute()
    except:
        getAccess()

但是 service.files().list(**param).execute() 行会产生以下错误消息:

Traceback (most recent call last):
  File "GoogleAuth.py", line 64, in <module>
    service.files().list(**param).execute()
  File "C:\Anaconda3\lib\site-packages\oauth2client\util.py", line 137, in     positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Anaconda3\lib\site-packages\googleapiclient\http.py", line 729, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError

我尝试过使用几种不同的组合,例如:

service.files().list().execute()
service.apps().list().execute()

但是我仍然收到相同的错误消息。知道发生了什么吗?

【问题讨论】:

  • An HttpError can contain a reason 提供更多洞察力。
  • 感谢您的回复我没有看到异常处理代码。我尝试实现它并将其与 try: service.files().list().execute() except Exception as ex: str(ex) 但是我收到以下错误消息 TypeError: the JSON object must是 str,而不是 'bytes' 我已经为此寻找解决方案并找到以下Stackoverflow 但是代码已经在使用 decode("utf-8") 所以我不确定出了什么问题:(
  • 显然代码是从 \Lib\site-packages\googleapiclient\errors.py 运行的 没有添加 .decode("utf-8") 。我已更正此问题并提取了 HttpError,即请求 googleapis.com/drive/v2/files?alt=json 时返回的 。我现在正在研究这个错误的实际含义以及如何解决它
  • 从另一个 Stackoverflow 线程中找到,我需要将我的 service() 调用转换为 service = build('drive', 'v2', http=http)
  • 您能说明一下您是如何解决最初的问题的吗?我现在遇到了同样的事情,首先是 googleapiclient.errors.HttpError ,其回溯到“TypeError;JSON 对象必须是 str,而不是 'bytes'”。感谢您的修复

标签: python-3.x google-api


【解决方案1】:

问题是

    service = build('drive', 'v2')

应该是

    service = build('drive', 'v2', http=http)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 2012-11-04
    • 2021-08-18
    • 2012-07-04
    • 1970-01-01
    • 2018-09-01
    相关资源
    最近更新 更多