我正在为我的rest_framework使用oauth2_provider。我正在尝试为我的api编写测试用例。我已获得访问令牌。但是我无法在APIClient中使用access token对用户进行身份验证,我希望在APIClient中使用这个curl命令。
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">发布于 2018-06-04 18:41:37
由于您在curl中使用Authorization: Bearer,因此还应将client.credentials与Bearer word一起使用,而不是Token
client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.access_token)https://stackoverflow.com/questions/50678609
复制相似问题