Base64编码HTTP_AUTHORIZATION标头DRF

Pro*_*eus 5 python django django-rest-framework

我正在尝试使用Django Rest Framework创建一个测试:

self.api = APIClient()
self.api.credentials(HTTP_AUTHORIZATION='client_id ' + str(self.client_id) + ' client_secret ' + str(self.client_secret))
response = self.api.post(url, payload)
Run Code Online (Sandbox Code Playgroud)

但我得到上述错误:

基本标头无效.凭据未正确base64编码.

我需要base64编码头吗?我以为这是为你完成的?使用curl测试同样的东西没有问题,即

curl -X POST -d "stuff=stuff" http://RqePZpAwyyVqal9D:18288GHFRb@127.0.0.1:8000/api/test/
Run Code Online (Sandbox Code Playgroud)

Joe*_*elm 1

事实上,您确实需要自己对身份验证字符串进行 Base64 编码。在这种情况下,我相信它应该看起来像:

import base64

auth = 'client_id %s client_secret %s' % (base64.b64encode(self.client_id),
                                          base64.b64encode(self.client_secret))
self.api.credentials(HTTP_AUTHORIZATION=auth)
Run Code Online (Sandbox Code Playgroud)