OAuth2客户端/appengine.py使用webapp2/python27/wsgi返回“InvalidResponseError:header值必须是str,get'unicode'”

2024-05-20 10:44:57 发布

您现在位置:Python中文网/ 问答频道 /正文

在此之前,我的问题类似于问题Pyramid on App Engine gets "InvalidResponseError: header values must be str, got 'unicode',以及几个{a2},但对我的情况没有任何帮助。另外,我在issue #254上没有答案(它本身看起来与#111相似),所以我在这里尝试。在

在本地GAE上,下面的简单示例(简化的python27版本的this sample)返回InvalidResponseError: header values must be str, got 'unicode',尽管我的代码是而不是执行任何unicode头设置。更确切地说,我期望的结果是Hello,而我得到了:

Internal Server Error
    The server has either erred or is incapable of performing the requested operation.
    Traceback (most recent call last):
      File "/home/ronj/.gae/lib/webapp2-2.5.2/webapp2.py", line 1546, in __call__
        return response(environ, start_response)
      File "/home/ronj/.gae/lib/webob_0_9/webob/__init__.py", line 2000, in __call__
        start_response(self.status, self.headerlist)
      File "/home/ronj/.gae/google/appengine/runtime/wsgi.py", line 156, in _StartResponse
        (_GetTypeName(value), value, name))
    InvalidResponseError: header values must be str, got 'unicode' (u'https://accounts.google.com/o/oauth2/auth?state=http%3A%2F%2Flocalhost%3A8080%2F&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Foauth2callback&response_type=code&client_id=xxxxxxxxxxxx.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&access_type=offline') for 'Location'

有什么想法吗?我在Ubuntu12.10x64上的python2.7.3上使用gae1.7.5。在

编辑:Jonas在issue #254:中提供了一个答案:“在OAuth2WebServerFlow上生成url的方法中添加一些str()应该比较容易。在oauth2client的第830行返回之前用str()换行/客户端.py“
→看起来不错,但我该如何实现呢?我同意我可以修改我安装GAE的本地机器上的文件,但是一旦部署,就会使用Google的GAE,对吗?我怎样才能覆盖它?(对于菜鸟的问题我很抱歉)

谢谢你的帮助!在


应用程序yaml

^{pr2}$

你的app.py

import webapp2, os, httplib2
from apiclient.discovery import build
from oauth2client.appengine import oauth2decorator_from_clientsecrets
from google.appengine.api import memcache

CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
MISSING_CLIENT_SECRETS_MESSAGE = "Warning: Please configure OAuth 2.0"
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

http = httplib2.Http(memcache)
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=http)
decorator = oauth2decorator_from_clientsecrets(
    CLIENT_SECRETS,
    scope=YOUTUBE_READ_WRITE_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)


class MainPage(webapp2.RequestHandler):

  @decorator.oauth_required
  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.write('Hello')

main = webapp2.WSGIApplication([('/', MainPage)], debug=True)

Tags: frompyimportselfclientapiyoutuberesponse
3条回答

另一个答案。。。添加str()可以解决问题,但不能解决根本原因。我花了好几个小时试图弄清楚为什么一个特定的重定向会引发这个错误,而其他的却没有,直到发现错误重定向的URL缺少了首字母“/”。在

我不知道为什么会出现这种情况-不完整路径的处理方式可能与完整路径不同。但如果遇到此错误,请尝试更改:

self.redirect('home.view')

收件人:

^{pr2}$

即使我已经在使用apiclient的最新版本,我也遇到了同样的问题。在

对我来说解决这个问题的答案是发布在这里的问题跟踪器http://code.google.com/p/google-api-python-client/issues/detail?id=254

不起作用

flow = client.flow_from_clientsecrets(CLIENT_SECRETS,scope=scopes)
callback = self.request.relative_url('/oauth2callback')
auth_url = flow.step1_get_authorize_url(callback)
return self.redirect(auth_url)

工作

^{pr2}$

注意str()包装了flow.step1_get_authorize_网址呼叫

我使用的是GAE捆绑版本的pythonapi,显然不应该这样做。在安装项目download page上的google-api-python-client-1.1之后,一切都按预期工作。在

相关问题 更多 >