gpt4 book ai didi

python - 如何在 App Engine 上将 PATCH 方法与适用于 Python 的 Google API 客户端库一起使用?

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:40 25 4
gpt4 key购买 nike

我正在尝试使用适用于 Python 的 Google API 客户端库来重命名驱动文件,这里是重命名函数:

def rename_file(service, file_id, new_title):
"""Rename a file.

Args:
service: Drive API service instance.
file_id: ID of the file to rename.
new_title: New title for the file.
Returns:
Updated file metadata if successful, None otherwise.
"""
try:
file = {'title': new_title}
# Rename the file.
updated_file = service.files().patch(
fileId=file_id,
body=file,
fields='title').execute()
return updated_file
except errors.HttpError, error:
logging.error('An error occurred: %s' % error)
return None

但是得到错误,这里是trackback:

    Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/myproduct/libs/oauth2client/appengine.py", line 469, in check_oauth
return method(request_handler, *args, **kwargs)
File "/myproduct/main.py", line 31, in get
new_title="file new name")
File "/myproduct/gapi.py", line 148, in rename_file
fields='title').execute()
File "/myproduct/libs/oauth2client/util.py", line 120, in positional_wrapper
return wrapped(*args, **kwargs)
File "/myproduct/libs/apiclient/http.py", line 676, in execute
headers=self.headers)
File "/myproduct/libs/oauth2client/util.py", line 120, in positional_wrapper
return wrapped(*args, **kwargs)
File "/myproduct/libs/oauth2client/client.py", line 420, in new_request
redirections, connection_type)
File "/myproduct/libs/httplib2/__init__.py", line 1588, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/myproduct/libs/httplib2/__init__.py", line 1336, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/myproduct/libs/httplib2/__init__.py", line 1273, in _conn_request
conn.request(method, request_uri, body, headers)
File "/myproduct/libs/httplib2/__init__.py", line 1112, in request
validate_certificate=self.validate_certificate)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/urlfetch.py", line 265, in fetch
allow_truncated, follow_redirects, validate_certificate)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/urlfetch.py", line 286, in make_fetch_call
raise InvalidMethodError('Invalid method %s.' % str(method))
InvalidMethodError: Invalid method PATCH.

那么直接使用rename_file函数应该怎么做呢?

最佳答案

如错误消息所示,urlfetch 尚不支持 PATCH,但是 issue已向 App Engine 团队提交并确认该问题。

与此同时,您可以修补 httplib2 以发送 X-HTTP-METHOD-OVERRIDE header ,而不是使用 PATCH 方法,如补丁中所示 filed针对 httplib2

由于该补丁引用了一些旧代码,因此我在此处提供了相关代码段:

UNAUTHORIZED_METHODS = ['patch']
HTTP_METHOD_OVERRIDE_HEADER = 'X-HTTP-Method-Override'
HTTP_OVERRIDE_METHOD = 'POST'

class AppEngineHttpConnection(httplib.HTTPConnection):
...
def request(self, method, url, body=None, headers={}):
if method.lower() in UNAUTHORIZED_METHODS:
# No side-effects on provided headers.
new_headers = {}
new_headers.update(headers)
new_headers[HTTP_METHOD_OVERRIDE_HEADER] = method
method = HTTP_OVERRIDE_METHOD
headers = new_headers
super(AppEngineHttpConnection, self).request(method, url, body, headers)

由于 request 现在只是在 httplib.HTTPConnection 中定义的那个,我们可以在更改我们需要的一个 header 的同时重新使用它。

HTTPS 版本需要类似的覆盖,因为它不继承自 AppEngineHttpConnection

关于python - 如何在 App Engine 上将 PATCH 方法与适用于 Python 的 Google API 客户端库一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14107336/

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