Python Requests with Django Rest Framework - 'detail': 'Authentication credentials were not provided'

Hanny

I've got a tiny function that just looks to get a response from my DRF API Endpoint.

My DRF settings look like this:

    "DEFAULT_AUTHENTICATION_CLASSES": [
        # Enabling this it will require Django Session (Including CSRF)
        "rest_framework.authentication.SessionAuthentication"
    ],
    "DEFAULT_PERMISSION_CLASSES": [
        # Globally only allow IsAuthenticated users access to API Endpoints
        "rest_framework.permissions.IsAuthenticated"
    ],

I'm using this to try and hit the endpoint:

def get_car_details(car_id):
    headers = {"X-Requested-With": "XMLHttpRequest"}
    api_app = "http://localhost:8000/"
    api_model = "cars/"
    response = requests.get(api_app + api_model + str(car_id), headers=headers)
    json_response = response.json()
    return json_response

I keep getting 'detail': 'Authentication credentials were not provided'

Do I need to generate a CSRF token and include it in a GET request? The only time this gets hit is when a user goes to a view that requires they are logged in. Is there a way to pass that logged-in user to the endpoint??

paperreduction

When you make your request from the get_car_details function you need to be sure that the request is authenticated. This does not look like a CSRF issue.

When using session based authentication a session cookie is passed back after logging in. So before you call get_car_details you would need to first make a request to login, keep that session, and use that session when calling the API.

Requests has a session class requests.Session() that you can use for this purpose. Details here: https://docs.python-requests.org/en/latest/user/advanced/

Many people find token based authentication easier partly a session cookie does not need to be maintained.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django Rest Framework - Authentication credentials were not provided

Django Rest Framework Token Authentication

Authentication credentials were not provided with djangorestframework-jwt

Authentication credentials were not provided django-rest-auth

DRF API endpoint getting "Authentication credentials were not provided"

Authentication credentials were not provided. when deployed to AWS

Authentication credentials were not provided when sending request from React JS axios

django-oauth-toolkit; Django Rest Framework - Authentication credentials were not provided

DRF Token Authentication: { "detail": "Authentication credentials were not provided." }

Permission classess decorator is ignored. "Authentication credentials were not provided" response

Authentication credentials were not provided drf

Auth error with POST request: "Authentication credentials were not provided" using Axios, but works using POSTMAN

{detail: "Authentication credentials were not provided."} on get request with simplejwt

How to customize [Authentication credentials were not provided] error message in Django rest framework

Django REST framework no authentication details

django rest framework no authentication no permission

Seafile installation - {"detail":"Authentication credentials were not provided."}

Django : "detail": "Authentication credentials were not provided."

"detail": "Authentication credentials were not provided."

Authentication credentials were not provided with Django Rest Framework JWT

Django Authentication credentials were not provided

"Authentication credentials were not provided." in DRF

"detail": "Authentication credentials were not provided." for general views

Python plotly choropleth map visualization , authentication credentials were not provided error

Forbidden: /api/v1.0/user/create-user/ & { "detail": "Authentication credentials were not provided." } DJANGO

How to solve "detail": "Authentication credentials were not provided." error for Class-based APIView Django REST Framework?

How to solve the error "Authentication credentials were not provided" while running a CURL get request in php?

"detail": "Authentication credentials were not provided." when I want to signup user

How to fix {detail: Authentication credentials were not provided.} in Django Rest Framework