djangodjango-rest-frameworkdjango-socialauthdjango-oauth

Django rest framework social oauth2 api url and response customization


I am making an API system which follows url like localhost:8080/api/v1/end_name and i'm using django-rest-framework-social-oauth2 library for social authentication also for my custom user authentication. The problem is they are providing api response for url like localhost:8080/auth/token in following format e.g.

{
"access_token": "........",
"expires_in": 36000,
"token_type": "Bearer",
"scope": "read write",
"refresh_token": "......"
}

but i need to customize it in my way as my response format is different. My one is like following..

{
    "error": false,
    "message": "User created successfully",
    "data": {
        "email": "localtestuse2@beliefit.com"
    }
}

i need the response in my data: {}. My one question is

My another question is


Solution

  • i came up with the solution eventually. To make custom response, i had to override their method and customize the response according to my need. Here the method that is invoked named TokenView. so i customized it in following way

    class UserLoginView(TokenView):
    @method_decorator(sensitive_post_parameters("password"))
    def post(self, request, *args, **kwargs):
        url, headers, body, status = self.create_token_response(request)
        # body is str here, we need to make it proper json
        data = json.loads(body)
    
        if status != 200:
            response = Response(makeContext(True, "Couldn't generated token", data))
        else:
            response = Response(makeContext(False, "Token generated successfully", data))
    
        response.accepted_renderer = JSONRenderer()
        response.accepted_media_type = "application/json"
        response.renderer_context = {}
        return response
    

    here makecontext is my customized json maker method.