Я использую Джанго 1.11.9.
Я хочу добавить client_id и client_secret в запрос POST django.
Вот как выглядит мой файл 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.
В чем может быть проблема? Как правильно обновить запрос?