- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Django 的新手,我正在尝试为后端创建一个 API 以供 iOS 客户端使用。目前我正在使用 curl 测试我的 API 访问权限。
我已经使用以下方法成功生成了访问 token :
curl -X POST -d "grant_type=password&username=example_user&password=example_password" http://clientID:clientSecret@localhost:8000/o/token/
生成以下响应 -
{
"access_token": "oAyNlYuGVk1Sr8oO1EsGnLwOTL7hAY",
"scope": "write read",
"expires_in": 36000,
"token_type": "Bearer",
"refresh_token": "pxd5rXzz1zZIKEtmqPgE608a4H9E5m"
}
然后我使用访问 token 尝试访问以下类 View :
from django.http import JsonResponse
from oauth2_provider.views.generic import ProtectedResourceView
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
class post_test(ProtectedResourceView):
def get(self, request, *args, **kwargs):
print(request.user)
return JsonResponse({'Message': "You used a GET request"})
def post(self, request, *args, **kwargs):
print(request.user)
return JsonResponse({'Message': 'You used a POST request'})
@method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs):
return super(post_test, self).dispatch(*args, **kwargs)
使用以下 curl 请求:
curl -H "Authorization: Bearer oAyNlYuGVk1Sr8oO1EsGnLwOTL7hAY" -X GET http://localhost:8000/api/post-test/
正确响应客户端:
{"Message": "You used a GET request"}
但是在控制台中,我期望 request.user
变量,我得到了 AnonymousUser
。
token 不是应该分配给 example_user
吗?这不应该是 request.user
返回的内容吗?
最佳答案
使用 request.resource_owner
而不是 request.user
。
Django OAuth 工具包区分与不记名 token 关联的用户和与可能附加到请求的任何其他 Django 身份验证关联的用户。具体来说,ProtectedResourceView
包括来自 ProtectedResourceMixin
的行为,其中包括以下调度方法:
def dispatch(self, request, *args, **kwargs):
# let preflight OPTIONS requests pass
if request.method.upper() == "OPTIONS":
return super().dispatch(request, *args, **kwargs)
# check if the request is valid and the protected resource may be accessed
valid, r = self.verify_request(request)
if valid:
request.resource_owner = r.user
return super().dispatch(request, *args, **kwargs)
else:
return HttpResponseForbidden()
关于python - Django oauth2 request.user 返回 AnonymousUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39151064/
if not request.user.is_authenticated: return None try: return ClientProfile.objects.get(user
我有以下型号 class Team(models.Model): name = models.CharField(max_length=100) members = models.Ma
我正在创建用户创建的所有帖子的摘要页面,并将这些帖子返回到摘要页面。我得到上面的错误? 它提示这一行: uploaded_aircraft = Aircraft.objects.filter(user
我将 Django 应用程序部署到远程服务器,但 tastypie 无法识别远程服务器上的登录用户。 我有一个 tastypie 资源,可以过滤属于当前登录用户的所有对象: def apply_a
使用 django-socialregistration,出现以下错误: 'AnonymousUser' object has no attribute 'backend' 如何, 我点击 faceb
我有以下输入新用户的注册 View 。 我想让它输入新用户,然后自动登录。 它通过用户记录保存,但在尝试登录时返回此错误: 'AnonymousUser' 对象没有属性 'backend' View
我有一个针对现有基于 spring 的应用程序的 spring 安全实现,无论我在登录页面提供什么,它总是返回匿名用户。 @Configuration @EnableWebSecurity @Enab
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我使用context_processorts.py将某种过滤器传递到整个项目中,但它仅在用户登录时有效,当没有用户登录时它会返回错误。这是我的文件: context_processors.py def
def gmail_authenticate(request): storage = DjangoORMStorage(CredentialsModel, 'id', request.user
我正在测试一个表单 当用户看到表单时,用户可能已登录,也可能是匿名用户 如果表单成功,用户将登录 如果表单失败,用户将保持原样 我想通过单元测试来确认此行为。 django.test.Client 确
我正在尝试在 Django 中创建一个表单,其中包含一个表单字段,该表单字段具有从 User 对象中提取的默认值。 表单.py class EvangelizedForm(forms.ModelFor
我正在尝试找出 request.user 究竟在何处/何时成为 AnonymousUser。我一直在搜索整个 Auth 后端,但我似乎找不到它。我找错地方了吗? 我知道每个未经过身份验证的用户都会成为
我关注了这个tutorial创建自定义用户。在views.py中,我创建了一个通过用户ID(即/users/42)预设用户信息的方法,但结果是AnonymousUser。我应该提到我试图获取的数据库记
我有以下网址: 主要urls.py: url(r'^accounts/profiles/', include('accounts.urls', namespace = 'accounts')), ac
我在我的 Django 应用程序中使用社交登录。因此,我在我的 settings.py 文件中添加了额外的后端。 AUTHENTICATION_BACKENDS = [ 'django.con
我正在使用 Django 2.2 和 DRF 在我的应用程序中,根据用户属性,我需要将 request.user 更改为不同的用户。 class CustomMiddleware: def __i
我一直在测试我的应用程序,陷入了与应用程序本身无关的错误。我有这样的单元测试: class TagSaveAndLoadTest(TestCase): fixtures = ['users.j
我正在努力解决 is_authenticated返回 True当我没有登录时: u = request.user if u.is_authenticated: raise Exception(
在我的 Django 站点中,我计划结合使用 message_set 和一些精美的 UI 来警告用户“您的操作已成功完成”或“您有三条新消息”等内容。 当我尝试警告尝试登录的用户他提供的用户/通行证是
我是一名优秀的程序员,十分优秀!