Angularjs &引用;不支持的“U型授权”;使用$resource进行用户身份验证时出错
我的问题Angularjs &引用;不支持的“U型授权”;使用$resource进行用户身份验证时出错,angularjs,oauth-2.0,Angularjs,Oauth 2.0,我的问题 app.factory('authService', ['$http', '$q', 'localStorageService', '$resource', '$rootScope', function ($http, $q, localStorageService, $resource, $rootScope) { var authServiceFactory = {}; var _authentication = { isAuth: fa
app.factory('authService', ['$http', '$q', 'localStorageService', '$resource', '$rootScope',
function ($http, $q, localStorageService, $resource, $rootScope) {
var authServiceFactory = {};
var _authentication = {
isAuth: false,
userName: ""
};
var url = function (relativeUrl) {
return $rootScope.apiBaseUrl + '/api/' + relativeUrl;
};
var authResource = $resource(url('account/:id'), null, {
register: {
method: 'POST',
url: url('account/register')
},
login: {
method: 'POST',
url: $rootScope.apiBaseUrl + '/token'
}
},
{
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/x-www-form-urlencoded'
}
});
var _login = function (loginData) {
// var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;
loginData.grant_type = "password";
var deferred = $q.defer();
authResource.login($.param(loginData)).$promise
.then(function (response) {
localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName });
_authentication.isAuth = true;
_authentication.userName = loginData.userName;
deferred.resolve(response);
});
return deferred;
};
var _fillAuthData = function () {
var authData = localStorageService.get('authorizationData');
if (authData) {
_authentication.isAuth = true;
_authentication.userName = authData.userName;
}
}
authServiceFactory.saveRegistration = _saveRegistration;
authServiceFactory.login = _login;
authServiceFactory.logOut = _logOut;
authServiceFactory.fillAuthData = _fillAuthData;
authServiceFactory.authentication = _authentication;
return authServiceFactory;
}]);
使用$post一切正常,但当我将其更新为$resource时
我收到不支持的授权类型的错误
我尝试了许多解决方案,但无法解决这个问题
下面是我的代码
控制器
$scope.login = function () {
authService.login($scope.loginData).promise.then(function (response) {
if (document.referrer != "" && document.referrer != window.location.href) {
window.location.href = document.referrer;
}
else {
window.location.href = "/Roles/Index"
}
}
};
服务
app.factory('authService', ['$http', '$q', 'localStorageService', '$resource', '$rootScope',
function ($http, $q, localStorageService, $resource, $rootScope) {
var authServiceFactory = {};
var _authentication = {
isAuth: false,
userName: ""
};
var url = function (relativeUrl) {
return $rootScope.apiBaseUrl + '/api/' + relativeUrl;
};
var authResource = $resource(url('account/:id'), null, {
register: {
method: 'POST',
url: url('account/register')
},
login: {
method: 'POST',
url: $rootScope.apiBaseUrl + '/token'
}
},
{
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/x-www-form-urlencoded'
}
});
var _login = function (loginData) {
// var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;
loginData.grant_type = "password";
var deferred = $q.defer();
authResource.login($.param(loginData)).$promise
.then(function (response) {
localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName });
_authentication.isAuth = true;
_authentication.userName = loginData.userName;
deferred.resolve(response);
});
return deferred;
};
var _fillAuthData = function () {
var authData = localStorageService.get('authorizationData');
if (authData) {
_authentication.isAuth = true;
_authentication.userName = authData.userName;
}
}
authServiceFactory.saveRegistration = _saveRegistration;
authServiceFactory.login = _login;
authServiceFactory.logOut = _logOut;
authServiceFactory.fillAuthData = _fillAuthData;
authServiceFactory.authentication = _authentication;
return authServiceFactory;
}]);
我从这么多次尝试中发现了什么?
我发现,我的请求没有任何标题。默认数据转换是json for
$http
@charlietfl,实际上我是angular方面的新手,您能给我一些指导吗?您是否尝试过将工作版本的http身份验证请求与有问题的$resource
变体进行比较?是的,有几个不同之处,如从Post到Optional的调用方法和访问头等。。