1

Working with Django REST Framework JWT at server side and AngularJs and Restangular at client side, when I set username and password in header:

RestangularProvider.setDefaultHeaders({'Authorization': 'Basic ' +$base64.encode('username:password')}); 

it works correctly.

But when I set token as part of header :

RestangularProvider.setDefaultHeaders({Authorization:'Bearer '+ StorageService.get("access_token")}); 

I get 401 (UNAUTHORIZED) error.

My viewset:

class BookViewSet(DefaultsMixin, viewsets.ModelViewSet): queryset = Book.objects.all() permission_classes = (permissions.IsAuthenticated,) serializer_class = BookSerializer 

Setting:

REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ), 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', 'rest_framework_jwt.authentication.JSONWebTokenAuthentication' ), 

}

Request Headers (part)

Request Headers Accept:application/json, text/plain, */* Accept-Encoding:gzip, deflate, sdch Authorization:Bearer"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRqYW5hdGkxMUBnbWFpbC5jb20iLCJleHAiOjE0NDc2NzYxNDcsInVzZXJfaWQiOjEwOSwidXNlcm5hbWUiOiJwcDEiLCJvcmlnX2lhdCI6MTQ0NzY3MzE0N30.ImkGmK9_TNU5lCvcGzgNi-1XS_Q4c_AxHSnJOcjn6O8" ... 

Thanks for your help!

2 Answers 2

4

I assume this is the JWT token type. In that case you should use the following format in your header:

Authorization: JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRqYW5hdGkxMUBnbWFpbC5jb20iLCJleHAiOjE0NDc2NzYxNDcsInVzZXJfaWQiOjEwOSwidXNlcm5hbWUiOiJwcDEiLCJvcmlnX2lhdCI6MTQ0NzY3MzE0N30.ImkGmK9_TNU5lCvcGzgNi-1XS_Q4c_AxHSnJOcjn6O8 
Sign up to request clarification or add additional context in comments.

Comments

1

In my case, the token was not being sent.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.