【发布时间】:2018-11-13 16:08:34
【问题描述】:
我将oauth2_provider 用于我的rest_framework。我正在尝试为我的 api 编写测试用例。我已获得访问令牌。但我无法在APIClient 中使用access token 对用户进行身份验证
我希望让这个 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)
这里是sn-p的部分
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">
【问题讨论】:
标签: django django-rest-framework django-testing django-unittest oauth-provider