I created an API with basic authentication and I was wondering how can i consume it with an username and password
this is my endpoint
http://127.0.0.1:8000/Clerk/
I try to consume it by
def display_clerk_view(request):
displaytable = requests.get('http://127.0.0.1:8000/Clerk/')
jsonobj = displaytable.json()
return render(request, 'clearance/clerk_view.html', {"ClerkClearanceItems": jsonobj})
this block of code and it's giving me error Unauthorized: /Clerk/
I'm planning to use django all-auth with google as provider but for now I want to know how consuming api with authentication works
this is the tutorial i'm following https://www.geeksforgeeks.org/basic-authentication-django-rest-framework/?tab=article
CodePudding user response:
in this case, you are requesting to "/Clerk/" but it's returning unauthorized. There are two ways to fix this issue:
- Remove authentication permission from your view (if function-based view, remove the decorator login_required or is_authenticated condition from the view. If class-based view, add "permission_classes = [AllowAny, ]")
You'r given link: There are used a decorator called "permission_classes", You can replace "@permission_classes([IsAuthenticated])" with "@permission_classes([AllowAny])
- If you want authorized action, you can just create a token by username and password.
There are many ways to create tokens, the most popular one is simple-jwt.To learn more follow the link: Simple JWT DOCS