rauth: JSON 对象必须是 str,而不是 'bytes'
rauth: the JSON object must be str, not 'bytes'
尝试在 Django 1.10 + Python 3 上实施 OAuth2 身份验证,我收到此错误:
rauth: the JSON object must be str, not 'bytes'
它发生在调用的线路上:session = sso.get_auth_session(data=data, decoder=json.loads)
。
如果我删除 decoder=json.loads
,我会收到以下错误:
Decoder failed to handle access_token with data as returned by provider. A different decoder may be needed.
这是我的方法:
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/')
第一次访问视图重定向到授权URL(没有GET['code']
)。接下来,再次访问它(使用 GET['code']
)以处理授权码和身份验证。
有什么想法吗?
我仍然认为应该有更直接的方法或错误修复,但这是我自己的解决方法:
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)
问题是 get_auth_session
默认情况下将响应中的字节流解码为 UTF-8。如果您指定一个新的解码器,您将覆盖它。所以我想我们在这里都需要 (UTF-8 + JSON).
尝试在 Django 1.10 + Python 3 上实施 OAuth2 身份验证,我收到此错误:
rauth: the JSON object must be str, not 'bytes'
它发生在调用的线路上:session = sso.get_auth_session(data=data, decoder=json.loads)
。
如果我删除 decoder=json.loads
,我会收到以下错误:
Decoder failed to handle access_token with data as returned by provider. A different decoder may be needed.
这是我的方法:
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/')
第一次访问视图重定向到授权URL(没有GET['code']
)。接下来,再次访问它(使用 GET['code']
)以处理授权码和身份验证。
有什么想法吗?
我仍然认为应该有更直接的方法或错误修复,但这是我自己的解决方法:
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)
问题是 get_auth_session
默认情况下将响应中的字节流解码为 UTF-8。如果您指定一个新的解码器,您将覆盖它。所以我想我们在这里都需要 (UTF-8 + JSON).