Python 3.x Python-使用google drive API时出现HttpError

Python 3.x Python-使用google drive API时出现HttpError,python-3.x,google-api,Python 3.x,Google Api,我正在使用Python3.4来利用GoogleAPI从用户的Google驱动器访问和读取文件。如果用户在使用应用程序之前就应该拥有凭据文件,那么我希望能够通过尝试列出用户驱动器上的文件来测试凭据是否仍然有效。这个想法是,如果这个错误,那么应用程序知道它需要再次请求访问 经过大量搜索后,我尝试从以下示例中拼凑代码: 我目前有以下几段代码: import httplib2 from apiclient.discovery import build from oauth2client.file i

我正在使用Python3.4来利用GoogleAPI从用户的Google驱动器访问和读取文件。如果用户在使用应用程序之前就应该拥有凭据文件,那么我希望能够通过尝试列出用户驱动器上的文件来测试凭据是否仍然有效。这个想法是,如果这个错误,那么应用程序知道它需要再次请求访问

经过大量搜索后,我尝试从以下示例中拼凑代码:

我目前有以下几段代码:

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 = build('drive', 'v2')
应该是

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

谢谢你的回复,我还没有看到异常处理代码。我试图实现它并将其与try:service.files().list().execute()一起使用,但异常为ex:str(ex),但是我收到了以下错误消息TypeError:JSON对象必须是str,而不是“bytes”,我在上面寻找解决方案并找到了以下内容,但是代码已经在使用decode(“utf-8”)所以我不确定出了什么问题:(显然,代码是从\Lib\site packages\googleapiclient\errors.py运行的,没有.decode(“utf-8”)已添加。我已更正此错误并提取了HttpError,即。我现在正在研究此错误的实际含义,以及如何从另一个Stackoverflow线程中修复此错误,我需要将service()调用转换为service=build('drive','v2',http=http)你能澄清一下你是如何解决最初的问题的吗?我现在也遇到了同样的问题,第一个是GoogleAppClient.errors.HttpError,回溯到“TypeError;JSON对象必须是str,而不是“bytes”。如果你能解决这个问题,我将不胜感激
    service = build('drive', 'v2', http=http)