Search code examples
djangodjango-rest-frameworkdjango-authenticationdjango-oscar

django rest frame work : oscarapi authentication is not working


I am facing a weired issue when I call oscarapi calls through ios. It is working fine with POSTMAN. But when I call through xcode, it shows the following error.

HTTP/1.1 401 Authorization Required
Connection: Keep-Alive
Content-Type: application/json
Server: Apache/2.4.7 (Ubuntu)
Allow: GET, HEAD, OPTIONS
Vary: Accept,Authorization,Cookie
Date: Thu, 20 Apr 2017 13:55:13 GMT
X-Frame-Options: SAMEORIGIN
Www-Authenticate: Bearer realm="api"
Keep-Alive: timeout=5, max=100
Transfer-Encoding: Identity

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

I sending the heading authrization as follows. This is working fine in postman.

 Authorization: Bearer iw7cGIz4uF036j7VbpGCXbceCCbbD1

My urls.py

url(r'^api/v1/categories', views.CategoryListCustom.as_view(), name='category-list')

views.py

class CategoryListCustomd(generics.ListAPIView):
def get_queryset(self):
    queryset = Category.objects.all()
    return queryset
serializer_class = CustomCategorySerializer

Solution

  • I had the same issue, while inspecting the call log, I found that my call was 301 redirecting as follows -

    "GET /api/v1/basket HTTP/1.1" 301 0
    "GET /api/v1/basket/ HTTP/1.1" 200 401
    

    and was returning an "Authentication credentials were not provided." error. Appending a "/" in url solved the issue.

    The reason I suppose, 301 redirection not sending Authorization header in request. Hope this may help