我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用rest_framework.authentication.TokenAuthentication()。
def get_authentication_introspectors(view): """ Get View Authentication Introspectors :param view: DjangoRestFramework View :return: list of authentication introspectors :rtype: list """ from rest_framework import authentication authenticators_map = { authentication.BasicAuthentication: BasicAuthenticationIntrospector, authentication.TokenAuthentication: TokenAuthenticationIntrospector, } authenticators = getattr(view, 'authentication_classes', []) introspectors = [] for authenticator in authenticators: introspectors.append( authenticators_map.get(authenticator, BaseAuthenticationIntrospector)(authenticator) ) return introspectors
def reset(request): # Only run when in DEBUG mode! It's only used for tests if not settings.DEBUG: return HttpResponseBadRequest("Only allowed in debug mode.") if not request.user.is_authenticated(): ret = TokenAuthentication().authenticate(request) if ret is None: return HttpResponseBadRequest("Couldn't authenticate") login(request, ret[0]) # Only allow local users, for extra safety if not request.user.email.endswith('@localhost'): return HttpResponseBadRequest("Endpoint not allowed for user.") # Delete all of the journal data for this user for a clear test env request.user.journal_set.all().delete() request.user.journalmember_set.all().delete() try: request.user.userinfo.delete() except ObjectDoesNotExist: pass return HttpResponse()