我正在使用 django 1.11.9
我想将 client_id 和 client_secret 添加到 django POST 请求中。
这是我的 middleware.py 文件的样子:
class LoginMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# auth_header = get_authorization_header(request)
# Code to be executed for each request before
# the view (and later middleware) are called.
#Add Django authentication app client data to the request
request.POST = request.POST.copy()
request.POST['client_id'] = '12345678'
request.POST['client_secret'] = '12345678'
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
当我用调试器检查中间件时,它正在被成功处理。当一个视图被调用时,请求中缺少“client_id”和“client_secret”字段。
经过一些试验,我发现请求没有得到更新,当它在不同的视图中调用时,它返回旧值。
我稍后在 rest_framework_social_oauth2 中使用请求。这就是“client_id”和“client_secret”消失的时候。
class ConvertTokenView(CsrfExemptMixin, OAuthLibMixin, APIView):
"""
Implements an endpoint to convert a provider token to an access token
The endpoint is used in the following flows:
* Authorization code
* Client credentials
"""
server_class = SocialTokenServer
validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
oauthlib_backend_class = KeepRequestCore
permission_classes = (permissions.AllowAny,)
def post(self, request, *args, **kwargs):
import pdb ; pdb.set_trace()
# Use the rest framework `.data` to fake the post body of the django request.
request._request.POST = request._request.POST.copy()
for key, value in request.data.items():
request._request.POST[key] = value
url, headers, body, status = self.create_token_response(request._request)
response = Response(data=json.loads(body), status=status)
for k, v in headers.items():
response[k] = v
return response
我需要将 client_id 和 client_secret 添加到请求正文中,以便稍后由 rest_framework_social_oauth2 使用。
可能是什么问题呢?如何正确更新请求?