尝试在 Django 1.10 + Python 3 上实现 OAuth2 身份验证,我收到此错误:
rauth:JSON 对象必须是 str,而不是 'bytes'
它发生在线调用:session = sso.get_auth_session(data=data, decoder=json.loads)
。
如果我删除decoder=json.loads
,我会收到以下错误:
解码器无法使用提供者返回的数据处理 access_token。可能需要不同的解码器。
这是我的方法:
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']
)来处理授权代码并进行身份验证。
有任何想法吗?