Login to browsable API with token from custom authentication

401 views
Skip to first unread message

Sven Mäurer

unread,
Mar 11, 2015, 12:30:17 PM3/11/15
to django-res...@googlegroups.com
I have created an own ApiView for a login that generates a token for the user. In a REST client I can use this token together with the TokenAuthentication but in the browsable API not. Can I add a login form or do I have to hardcode the token somewhere?

Filipe Ximenes

unread,
Mar 11, 2015, 1:30:26 PM3/11/15
to django-res...@googlegroups.com
The easiest way is to also allow SessionAuthentication as well as TokenAuthentication in your view.
This can be done globally in your settings: 

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    )
}

or in your view:

class MyView(APIView):
    authentication_classes = (TokenAuthentication, SessionAuthentication)
    ...




On Wed, Mar 11, 2015 at 1:30 PM, Sven Mäurer <maeure...@gmail.com> wrote:
I have created an own ApiView for a login that generates a token for the user. In a REST client I can use this token together with the TokenAuthentication but in the browsable API not. Can I add a login form or do I have to hardcode the token somewhere?

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
  
Filipe Ximenes
Vinta Software Studio
+55 (81) 8245-9204

Sven Mäurer

unread,
Mar 11, 2015, 1:36:50 PM3/11/15
to django-res...@googlegroups.com
It does not change anything. I still get the message that the Authentication credentials are not provided.


Am Mittwoch, 11. März 2015 18:30:26 UTC+1 schrieb Filipe Ximenes:
The easiest way is to also allow SessionAuthentication as well as TokenAuthentication in your view.
This can be done globally in your settings: 

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    )
}

or in your view:

class MyView(APIView):
    authentication_classes = (TokenAuthentication, SessionAuthentication)
    ...



On Wed, Mar 11, 2015 at 1:30 PM, Sven Mäurer <maeure...@gmail.com> wrote:
I have created an own ApiView for a login that generates a token for the user. In a REST client I can use this token together with the TokenAuthentication but in the browsable API not. Can I add a login form or do I have to hardcode the token somewhere?

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Filipe Ximenes

unread,
Mar 11, 2015, 1:50:23 PM3/11/15
to django-res...@googlegroups.com
Try making login in Django admin site and then accessing the browsable api page again.
-- 
Reply all
Reply to author
Forward
0 new messages