Reputation: 247
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 json
# 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.
Any ideas?
Upvotes: 1
Views: 1182
Reputation: 247
I still think there should be something more straight forward or a bug fix to this, but here's my own workaround:
def oauth_decode(data):
import json
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).
Upvotes: 2