我正试图获得一些访问令牌来消费远程应用程序中托管的一些rest api,当我从curl代理和一些插件(如Open HttpRequester)发送请求时,我成功地获得了令牌,并且我成功地消费了rest api。
但当我试图从一个角度脚本函数请求令牌时,我收到了401未经授权的消息。
我想要一个angular js-post方法中的curl请求。
curl -X POST -vu clientapp:123456 http://localhost:8080/oauth/token -H "Accept: application/json" -d "password=spring&username=roy&grant_type=password&scope=read%20write&client_secret=123456&client_id=clientapp".
我不知道如何在post-angular方法中包含clientapp:123456对。
这是脚本
var app = angular.module("app", []);
app.controller("MyController", function($scope, $http) {
$scope.obtain_token = function() {
$http({
method : 'POST',
url : 'http://clientapp:123456@localhost:8080/oauth/token',
data: {
username:'roy',
password: 'spring',
client_id: 'clientapp',
client_secret: '123456',
scope: 'write'
},
transformRequest: function(obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}}).success(function (data, status, headers, config){
alert(data.access_token);
}).error(function (data, status, headers, config){
//alert("Errooooooooor");
});
};
});
我试过你的版本,但不起作用。
您可以发布到url:
http://clientapp:123456@localhost:8080/oauth/token
如果使用HTTP身份验证。