Debug School

rakesh kumar
rakesh kumar

Posted on

Dependencies to add token authentication and permission class in django

Step 1: Verify token authentication setup
Make sure you have configured the Token Authentication correctly in your Django project's settings. In your settings.py file, ensure that you have the following lines:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Obtain the token
Make sure you have obtained a valid token for authentication. You can use the Django REST Framework's token endpoint or any other authentication mechanism to obtain the token. Ensure that the token is generated successfully and valid.

Step 3: Include the token in the request header
When making the request to api_url, you need to include the token in the request header. Modify your views.py file to include the token in the request headers. Here's an example:

import requests

api_url = 'http://localhost:8000/myemployee/'
headers = {'Authorization': 'Token your_token'}

response = requests.get(api_url, headers=headers)
data = response.json()

print(data)  # Verify the output
Enter fullscreen mode Exit fullscreen mode

Replace 'your_token' with the actual token obtained in Step 2.

Step 4: Verify API URL and permissions
Ensure that the api_url points to the correct endpoint in your Django project. Check the URL configuration and the corresponding view for any issues.

Also, make sure that the user associated with the token has the necessary permissions to access the myemployee/ endpoint. You can check the user's permissions and make sure they have the required permissions to retrieve employee data.

Top comments (0)