0

I have a django server running on Linode, I'm trying to make a POST request but all I get is detail:"Authentication credentials were not provided."

I already made my research and whenever I have the server in production, (port 80), it doesn't work, as soon as I make ./manage.py runserver 0.0.0.0:8000 it works, when I point my fetch to that URL.

fetch(URL,
  method ='POST',
  headers = {
    'Authorization': `Token ${this.props.token}` ,
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body= JSON.stringify( {
    user: 10,
        voltage_coil_1: 1,
        voltage_coil_2: 1,
        voltage_generated_by_user: 1,
        activity:1,
        datetime:null
})
)
  .then(res => res.json())
  .then(console.log)

My expected results are a positive answer, but whenever I'm in production, I keep getting this error.

Server Response

2 Answers2

0

If you are using Apache as a web server in production mode, you should set WSGIPassAuthorization to 'On'.

Take a look at this link,

Django Rest Framework {"detail":"Authentication credentials were not provided."}

Mehran Nouri
  • 136
  • 15
  • Still getting the same error, I already placed `WSGIPassAuthorization On`and doesn't work. – Samuel Gomez Apr 22 '19 at 17:23
  • Can you print the authorization header of your request in the server side? I think the API view does not receive the token – Mehran Nouri Apr 22 '19 at 17:32
  • Hi, I'm new at django, how do I print the authorization header? – Samuel Gomez Apr 22 '19 at 17:43
  • I think this will help you `request.META.get('HTTP_AUTHORIZATION', b'')` – Mehran Nouri Apr 22 '19 at 17:55
  • Ok. I did that, here is my code: ```class DataViewSet(viewsets.ModelViewSet): queryset = UserData.objects.all() serializer_class = DataSerializer def post(self, request): print (request.META.get('HTTP_AUTHORIZATION', b'')) content = { 'response': 'response', } return Response(content)``` But all I get is `Forbidden: /api/data/ [22/Apr/2019 18:43:14] "GET /api/data/ HTTP/1.1" 403 58` And again, thank you for your patience with me! :/ @MehranNouri – Samuel Gomez Apr 22 '19 at 18:45
  • what does it print to console? – Mehran Nouri Apr 22 '19 at 19:14
  • Nothing, all I get is `Forbidden: /api/data/ [22/Apr/2019 18:43:14] "GET /api/data/ HTTP/1.1" 403 58` – Samuel Gomez Apr 22 '19 at 20:25
0

I could solve this problem by adding the following on my view on Django:

class DataViewSet(viewsets.ModelViewSet):
    queryset = UserData.objects.all()
    serializer_class = DataSerializer
    authentication_classes = [TokenAuthentication, SessionAuthentication]
    permission_classes = (IsAuthenticated,)