gpt4 book ai didi

python - 列出 Google 云端硬盘文件时出错

转载 作者:太空宇宙 更新时间:2023-11-03 18:43:51 25 4
gpt4 key购买 nike

我正在尝试使用谷歌应用引擎在应用程序中列出我的所有谷歌驱动器文件。我正在使用 oauth 协议(protocol)来访问驱动器文件。单击列出文件的链接时,我收到一个错误,如下所示。

请帮忙。提前致谢。

这是我的程序

#!/usr/bin/env python
#
# Copyright 2013 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Starting template for Google App Engine applications.

Use this project as a starting point if you are just beginning to build a Google
App Engine project. Remember to download the OAuth 2.0 client secrets which can
be obtained from the Developer Console <https://code.google.com/apis/console/>
and save them as 'client_secrets.json' in the project directory.
"""

import httplib2
import logging
import os
from google.appengine.api import users
from apiclient import discovery
from oauth2client import appengine
from oauth2client import client
from google.appengine.api import memcache
import webapp2
import jinja2


JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
autoescape=True,
extensions=['jinja2.ext.autoescape'])

# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
# application, including client_id and client_secret, which are found
# on the API Access tab on the Google APIs
# Console <http://code.google.com/apis/console>
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')

# Helpful message to display in the browser if the CLIENT_SECRETS file
# is missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
<h1>Warning: Please configure OAuth 2.0</h1>
<p>
To make this sample run you will need to populate the client_secrets.json file
found at:
</p>
<p>
<code>%s</code>.
</p>
<p>with information found on the <a
href="https://code.google.com/apis/console">APIs Console</a>.
</p>
""" % CLIENT_SECRETS

http = httplib2.Http(memcache)
service = discovery.build('drive', 'v2', http=http)
decorator = appengine.oauth2decorator_from_clientsecrets(
CLIENT_SECRETS,
scope=[
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.appdata',
'https://www.googleapis.com/auth/drive.apps.readonly',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/drive.readonly',
'https://www.googleapis.com/auth/drive.scripts',
],
message=MISSING_CLIENT_SECRETS_MESSAGE)

class MainHandler(webapp2.RequestHandler):
@decorator.oauth_aware
def get(self):
user= users.get_current_user()
if user:
variables = {
'user': users.get_current_user(),
'url': decorator.authorize_url(),
'has_credentials': decorator.has_credentials()
}
template = JINJA_ENVIRONMENT.get_template('main.html')
self.response.write(template.render(variables))
else:
self.response.write(users.create_login_url("/"))

class listFile(webapp2.RequestHandler):
@decorator.oauth_aware
def get(self):
if not decorator.has_credentials():
self.reponse.write("<a href='")
self.reponse.write(decorator.authorize_url())
self.response.write(">Authorize</a>")
else:
a=service.files().list().execute()
self.response.write(a)
app = webapp2.WSGIApplication(
[
('/', MainHandler),
('/list', listFile),
(decorator.callback_path, decorator.callback_handler()),
],
debug=True)

我得到的错误是

Internal Server Error

The server has either erred or is incapable of performing the requested operation.

Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~test-ak2/1.371630947154576708/oauth2client/appengine.py", line 777, in setup_oauth
resp = method(request_handler, *args, **kwargs)
File "/base/data/home/apps/s~test-ak2/1.371630947154576708/main.py", line 104, in get
a=service.files().list().execute()
File "/base/data/home/apps/s~test-ak2/1.371630947154576708/oauth2client/util.py", line 132, in positional_wrapper
return wrapped(*args, **kwargs)
File "/base/data/home/apps/s~test-ak2/1.371630947154576708/apiclient/http.py", line 723, in execute
raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 401 when requesting https://www.googleapis.com/drive/v2/files?alt=json returned "Login Required">

最佳答案

当您请求文件列表时:

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

客户端库需要知道如何验证 HTTP 请求。它执行此操作的方式是使用一个特殊的 HTTP 对象,该对象知道如何向请求添加正确的凭据。

这可以在服务初始化时设置,也可以在调用execute() 时指定。

在您的情况下,您在初始化服务对象时指定它:

http = httplib2.Http(memcache)
service = discovery.build('drive', 'v2', http=http)

那很好 - 但是当你使用服务对象时,它不知道装饰器,所以你需要做的是从装饰器获取一个 HTTP 对象:

service.files().list().execute(http=decorator.http())

并将其传递给execute()方法。这将确保与登录用户关联的凭据添加到对 Drive API 的请求中。

关于python - 列出 Google 云端硬盘文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19978191/

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