我正在使用 Django 和 django-oauth-toolkit 为 Auth0 构建通用OAuth2 授权服务器。我计划使用 Django 服务器对用户进行多种不同服务的身份验证,使用 Auth0 作为中介。
我有一个在应用程序通过身份验证后调用的视图,我需要该视图来返回当前登录用户的详细信息。
网址.py:
# Return current logged in user
(r'^user/current/?$',
'my_app.views.current_user.get_user',
{},
'current_user'),
意见/current_user.py:
import json
from django.http import HttpResponse
from oauth2_provider.decorators import protected_resource
@protected_resource()
def get_user(request):
user = request.user
return HttpResponse(
json.dumps({
'username': user.username,
'email': user.email}),
content_type='application/json')
request.user
正在返回 AnonymousUser,而不是令牌所属的用户。
如何访问与 django-oauth-toolkit 颁发的令牌关联的 Django 用户?