- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用oauth2_provider
作为rest_framework
。我正在尝试为我的api编写测试用例。我已经获得了访问 token 。但是我无法使用access token
中的APIClient
对用户进行身份验证
我希望将这个curl命令与APIClient
一起使用。
curl -H "Authorization: Bearer <your_access_token>" http://localhost:8000/api/v1/users/current/
client.get('/api/v1/users/current/', headers={'Authorization': 'Bearer {}'.format(self.access_token)})
client.credentials(HTTP_AUTHORIZATION='Token ' + self.access_token)
from rest_framework.test import APIClient
from rest_framework.test import APITestCase
....
class APITest(APITestCase):
def setUp(self):
...
self.client = APIClient()
...
response = self.client.post('/api/v1/oauth2/token/', post_data)
self.access_token = response.json()['access_token']
def test_get_current_user(self):
client.get('/api/v1/users/current/', headers={'Authorization': 'Bearer {}'.format(self.access_token)})
<HttpResponseForbidden status_code=403, "text/html; charset=utf-8">
最佳答案
由于您正在curl中使用Authorization: Bearer
,因此还应该使用带client.credentials
字的Bearer
而不是Token
:
client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.access_token)
关于django - 如何在 `APIClient`中的 `django rest_framework test` header 中添加身份验证 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50678609/
我有一个 API 端点,它将使用 rest_framework 的 serializer.is_valid() 进行输入验证。它将返回自定义错误消息和响应。 serializer = FormSeri
我有一个名为 UserViewSet 的类: class UserViewSet(viewsets.ModelViewSet): queryset = UserData.objects.all
无法完成 rest_framework.authtoken 的迁移 Running migrations for authtoken: - Migrating forwards to 0001_ini
我注意到 Serializer 在拒绝带有未知字段的输入时并不是很严格: In [1]: from rest_framework import serializers In [2]: class Te
我正在尝试将我的 django 1.9 升级到 django 2.0。它对 GET() 工作正常,但在 POST() 中出现错误。我的 views.py 是:- class AccountInfoUp
这是一个简单的问题,但我对 django-rest-framework 很陌生。 我想知道是否有任何方法可以从序列化程序访问模型上定义的方法。? 说我有一个模型 class Listing(model
Django serializers 和有什么区别对比 rest_framework serializers ? 我制作了一个 webapp,我希望 API 成为项目创建的主要应用程序的一部分。不为
这是我的models.py的一部分: class Discount(models.Model): discount_id = models.AutoField(primary_key=True
在我的 View 函数中,我想返回一个 json 对象 (data1) 和一些文本/html(表单)。这可能吗? 我的代码 @api_view(['POST']) @permission_classe
当我按照 this tutorial 尝试使用 django rest 框架过滤器时出现错误.当我尝试导入库时出现错误: from django_filters.rest_framework impo
这是我的settings.py: INSTALLED_APPS = [ 'rest_framework', 'django.contrib.admin', 'django.co
我正在尝试创建一个继承自 permissions.BasePermission 的自定义权限类。我知道您可以覆盖自定义消息的 message 属性,但也可以覆盖 http 状态吗?我想为过期的链接返回
我怎样才能让 django 消息框架与 rest_framework 一起工作? 这是我的观点 @api_view(['GET', 'POST']) def myview(request):
我正在尝试为我的 Django REST API 设置 OAuth2 身份验证系统(使用 DjangoRestFramework 和 Django-Oauth-Toolkit)。我是按照官方文档写的,
我正在尝试运行 longclaw,但出现错误 $ python manage.py makemigrations catalog home Traceback (most recent call la
我正在尝试将几个文件发送到我的后端: class AccountsImporterTestCase(APITestCase): def test(self): data = [
我在 django docker 容器中的 virtualenv 中使用 pip 安装了带有 markdown 和 django filter 的 djangorestframework,通过 pip
我在 pythonanywhere.com 上部署我的应用程序时遇到问题。我已按照说明通过运行以下命令通过 pip 安装 teh django rest frameowrk 包 pip install
我有一个 API 需要以列表的形式返回一个 QuestionQueue 及其关联的 Question 对象。我让它工作得很好,它按照我想要的方式返回数据: class QuestionQueueSer
我是 Django 框架和 Django REST 框架的新手,但我可以运行基本设置和实现。当我为单个对象调用域时,它就像一个魅力,例如http://mydomain.com/location/1 (
我是一名优秀的程序员,十分优秀!