未提供的Django的REST框架身份验证凭据 [英] Django Rest Framework Authentication credentials were not provided

查看:5610
本文介绍了未提供的Django的REST框架身份验证凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Django的休息-auth的 Django的全AUTH 的DRF和Angularjs。在权威性方面的任何要求,我得到以下错误:

I'm using django-rest-auth with django-all-auth on DRF and Angularjs. 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:

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

我的 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 ='令牌'+ data.key;

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

试试这个:

httpProvider.defaults.headers.common.Authorization ='智威汤逊+ data.key;

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

问候。

这篇关于未提供的Django的REST框架身份验证凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆