Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the JSON object must be str, not 'bytes' #194

Open
leonardopessoa opened this issue Nov 7, 2016 · 1 comment
Open

the JSON object must be str, not 'bytes' #194

leonardopessoa opened this issue Nov 7, 2016 · 1 comment

Comments

@leonardopessoa
Copy link

Trying to implement a OAuth2 authentication on Django 1.10 + Python 3, I'm getting this error:

rauth: the JSON object must be str, not 'bytes'
It happens on the line calling: session = sso.get_auth_session(data=data, decoder=json.loads).

If i remove the decoder=json.loads, I get the following error:

Decoder failed to handle access_token with data as returned by provider. A different decoder may be needed.

Here is my method:

def login(request):
    """
    Login view
    @author: Leonardo Pessoa
    @since: 11/06/2016
    """
    from rauth import OAuth2Service
    from django.conf import settings
    import son

    # instantiating our login service with environment defined settings
    sso = OAuth2Service(
        name                = settings.LOGIN_SETTINGS['name'],
        client_id           = settings.LOGIN_SETTINGS['client_id'],
        client_secret       = settings.LOGIN_SETTINGS['client_secret'],
        access_token_url    = settings.LOGIN_SETTINGS['access_token_url'],
        authorize_url       = settings.LOGIN_SETTINGS['authorize_url'],
        base_url            = settings.LOGIN_SETTINGS['base_url'],
    )

    # check if we have a login code returned by OAuth
    get_code = request.GET.get('code', '')
    if get_code == '':
        params = {
            'redirect_uri'    : settings.LOGIN_SETTINGS['redirect'],
            'response_type'   : 'code'
        }
        url = sso.get_authorize_url(**params)

        return redirect(url)
    else:
        # we are in!
        data = {'code'          : get_code,
                'grant_type'    : 'authorization_code',
                'redirect_uri'  : settings.LOGIN_SETTINGS['redirect']
                }

        session = sso.get_auth_session(data=data, decoder=json.loads)

        return redirect('/logado/')

The view is accessed the first time to redirect to the authorize URL (no GET['code']). Next, it's accessed again (with the GET['code']) to handle the authorization code and authenticate.

Feel like there should be a mix of both solutions (encoding UTF-8 + json).

@leonardopessoa
Copy link
Author

I still think there should be something more straight forward or even a bug fix to this, but here's my own workaround:

def oauth_decode(data):
    import son

    new_data = data.decode("utf-8", "strict")

    return json.loads(new_data)
...
session = sso.get_auth_session(data=data, decoder=oauth_decode)

The problem is that get_auth_session decodes the byte stream from the response to UTF-8 by default. If you specify a new decoder, you override that. So I guess we need both here (UTF-8 + JSON).

annyanich pushed a commit to annyanich/mensa-tracker that referenced this issue Apr 15, 2017
I'm not sure why I only just now ran into this problem.  It may be due to
a change on Facebook's end.  Anyway, now login should be working again.  :)

See litl/rauth#194

(cherry picked from commit 648b32d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant