varundeboss / google-api-python-client

Automatically exported from code.google.com/p/google-api-python-client
Other
0 stars 0 forks source link

oauth2client/appengine.py returns "InvalidResponseError: header values must be str, got 'unicode'" with webapp2/python27/wsgi #254

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
  1. Try on a local GAE the example below (a simplified & python27-ified version of this sample: http://code.google.com/p/google-api-python-client/source/browse/samples/appengine/main.py )
  2. Open http://localhost:8080/

What is the expected output? What do you see instead?
 - Expected:
   "Hello"
 - Instead:
    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'

What version of the product are you using? On what operating system?
 - GAE 1.7.5
 - Python 2.7.3
 - Ubuntu 12.10 x64

Please provide any additional information below.

- Looks similar to issue #111
- I'm new to both GAE and python, sorry if the problem lies between my chair 
and my keyboard, and feel free to point me things I'm doing awfully wrong.
- app.yaml:
    application: yourapp
    version: 1
    runtime: python27
    api_version: 1
    threadsafe: true

    handlers:

    - url: /
      script: yourapp.main

    libraries:
    - name: webapp2
      version: latest

- yourapp.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)

Original issue reported on code.google.com by ronan.jo...@gmail.com on 24 Mar 2013 at 8:54

GoogleCodeExporter commented 8 years ago
I had a similar problem using the OAuth2WebServerFlow manually, and the 
redirecting to flow.step1_get_authorize_url()

A work around for me was to use str(flow.step1_get_authorize_url()).
Ie. force conversion to str instead of unicode string as this is what webapp2 
complains about.

It might be that webapp2 should be patched to handle unicode urls, I'm not 
sure. But in any event it should be relatively easy to add some str() into the 
methods on OAuth2WebServerFlow that generates URLs.

Ie. wrap with str() before return on line 1234 in oauth2client/client.py

Original comment by jopsen@gmail.com on 11 Apr 2013 at 7:39

GoogleCodeExporter commented 8 years ago
Thanks for the feedback! (un?)fortunately after a system reinstall/upgrade I am 
no longer able to reproduce my problem, so I won't be able to try the proposed 
solution. Please mark as closed, I'll re-open if needed.

Original comment by ronan.jo...@gmail.com on 30 Apr 2013 at 10:29

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Jonas, the problem resurfaced after I don't know what :-/ . I'm trying to 
follow your suggestion ("to add some str() into the methods on 
OAuth2WebServerFlow that generates URLs. Ie. wrap with str() before return on 
line 1234 in oauth2client/client.py"). And note: as of today's GAE 1.8.0, the 
line we are referring to is now line 830 in oauth2client/client.py: return 
urlparse.urlunparse(parts)

How am I supposed to implement that? I agree that I can modify the file on my 
local machine where I installed GAE, but once deployed it will be Google's GAE 
that will be used, right? How can I override it? (and sorry for the newbie 
question)

Original comment by ronan.jo...@gmail.com on 28 May 2013 at 2:58

GoogleCodeExporter commented 8 years ago
Adding a str() fixes the problem, but not the root cause. I spent hours trying 
to figure out why one particular redirect raised this error while others 
didn't, before noticing that the URL for the bad redirect was missing the 
initial "/".

I have no idea *why* this is the case - possibly an incomplete path is 
processed differently from a complete one. But if you hit this error try 
changing:

    self.redirect('home.view')

to:

    self.redirect('/home.view')

Original comment by g...@vig.co.nz on 9 Apr 2014 at 5:39