tutorial 沒有提供Django Rest Framework身份驗證憑證




rest_framework django2 (2)

我的問題在同一行中:

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

嘗試這個:

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

問候。

我正在使用django-rest-auth ,在DRF和Angularjs上使用django-all-auth 。 在任何關於auth的請求中,我得到以下錯誤:

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

經過這個,我意識到有很多類似的問題,所以把它們累積在一起,我嘗試了以下幾點:

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
                });
        });
    };
});

我哪裡錯了?


您的設置中有權限類別:

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

所以只有管理員用戶發出的請求正在經歷。