简体   繁体   English

未提供Django Rest Framework身份验证凭据

[英]Django Rest Framework Authentication credentials were not provided

I'm using django-rest-auth with django-all-auth on DRF and Angularjs. 我在DRF和Angularjs上使用django-rest-authdjango-all-auth On any request regarding auth, I get the following error: 在任何有关身份验证的请求中,我收到以下错误:

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

Going through SO, i've realised there are a lot of similar problems so accumulating them together, I tried the following: 通过SO,我意识到有很多类似的问题,所以积累起来,我尝试了以下几点:

settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    ...
    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
    ...
    'allauth',
    'allauth.account',
    'rest_auth.registration',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',

)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

DEFAULT_AUTHENTICATION = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.OAuth2Authentication',
        'rest_framework.authentication.TokenAuthentication',
    ),
}
REST_FRAMEWORK = {
   'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAdminUser'
   ),
}
AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend"
)
TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request",
    "django.contrib.auth.context_processors.auth",
    "allauth.account.context_processors.account",
    "allauth.socialaccount.context_processors.socialaccount",
)

REST_SESSION_LOGIN = False

My app.js file 我的app.js文件

sgApp.config(['$routeProvider','$locationProvider', '$httpProvider',
    function($routeProvider, $locationProvider, $httpProvider){
        $routeProvider.when('/',{
            templateUrl: '/static/partials/index.html',
            controller: 'indexCtrl'
        }).when('/abc',{
            templateUrl: 'static/partials/index.html'
        }).otherwise({
            redirectTo: '/'
        });
        $locationProvider.html5Mode(true).hashPrefix('!');
        $httpProvider.defaults.xsrfCookieName = 'csrftoken';
        $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
    }
]).controller('someCtrl', function($scope, $http, $httpProvider){
       $scope.login = function() {
           Facebook.login(function(response) {
            var postDict = {
                access_token: response.authResponse.accessToken
            }
            $http.post('/sgAuth/facebook/', postDict).
                success(function(data){
                    $httpProvider.defaults.headers.common.Authorization = 'Token ' + data.key;
                    $scope.loggedIn = true;
                    $scope.userDetails(); //function that gets user details
                });
        });
    };
});

Where am I going wrong? 我哪里错了?

I had same problem in your line: 你的问题也有同样的问题:

$httpProvider.defaults.headers.common.Authorization = 'Token ' + data.key; $ httpProvider.defaults.headers.common.Authorization ='Token'+ data.key;

Try this: 尝试这个:

httpProvider.defaults.headers.common.Authorization = 'JWT ' + data.key; httpProvider.defaults.headers.common.Authorization ='JWT'+ data.key;

Regards. 问候。

You have permission class in your settings: 您的设置中有权限类:

REST_FRAMEWORK = {
   'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAdminUser'
   ),
}

So only requests made by admin user are going through. 因此,只有管理员用户发出的请求才能通过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django Rest 框架:未提供身份验证凭据 - Django Rest Framework : Authentication credentials were not provided Django Rest 框架 - 未提供身份验证凭据 - Django Rest Framework - Authentication credentials were not provided Django Rest Framework-使用渲染器时未提供身份验证凭据 - Django Rest Framework - Authentication credentials were not provided when using renderer django rest 框架中未提供错误身份验证凭据 - getting error Authentication credentials were not provided in django rest framework Python 请求与 Django Rest 框架 - “详细信息”:“未提供身份验证凭据” - Python Requests with Django Rest Framework - 'detail': 'Authentication credentials were not provided' Django Rest Framework JWT未提供身份验证凭据 - Authentication credentials were not provided with Django Rest Framework JWT Django Rest Framework JWT“未提供身份验证凭据。”} - Django Rest Framework JWT “Authentication credentials were not provided.”} Django Rest Framework {“detail”:“未提供身份验证凭据。”} - Django Rest Framework {“detail”:“Authentication credentials were not provided.”} Django Rest 框架 allauth 未提供身份验证凭据 - Django Rest Framework allauth Authentication credentials were not provided 未提供Angular + Django REST身份验证凭据 - Angular + Django REST Authentication credentials were not provided
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM