python-3.xunicodeoauthflaskflask-oauthlib

Unicode-objects must be encoded before hashing when requesting data using Flask-OAuth


I'm integrating Google's login with a Flask site using Flask-OAuth.

Everything is working fine. I can authorise the login and get a token back etc without any difficulties. But when I use Flask-OAuth's get method to request the logged in user's email address I get an error saying:

TypeError: Unicode-objects must be encoded before hashing

I'm using Python3 and this has the smell of a Python version issue but I can't figure out what I'd need to change.

The code I'm using is this:

def get_additional_data(self):
    access_token = session.get('oauth_token')
    headers = {'Authorization': 'OAuth ' + access_token[0]}
    return self.service.get(
        'https://www.googleapis.com/oauth2/v1/userinfo', None,
        headers=headers)

I'm not sure what I can encode in that request. Even if I don't pass the headers I get the same error (rather than an invalid request or something like that).

I've run 2to3 on oauth2/__init__.py and the tweaks is suggests are very minor and shouldn't prevent the code from running in Python 3. Also, everything else OAuth2 related is working.


Solution

  • The bad news is that the solution to this problem is switching to Flask-OAuthlib.

    The good news is it required very few changes from Flask-OAuth to get it working.