无法从 angular 脚本获取访问令牌
failed to obtain access token from angular script
我正在尝试获取一些访问令牌以消耗远程应用程序中托管的一些剩余 apis,当我从 curl 代理和一些插件(如 Open HttpRequester)发送请求时,我成功获取了令牌, 我成功地消耗了剩下的 api.
但是当我尝试从 angular 脚本函数请求令牌时,我收到了 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");
});
};
});
我试过你的版本,但它不起作用。
您可以post到url:
http://clientapp:123456@localhost:8080/oauth/token
如果使用 HTTP 身份验证。
我正在尝试获取一些访问令牌以消耗远程应用程序中托管的一些剩余 apis,当我从 curl 代理和一些插件(如 Open HttpRequester)发送请求时,我成功获取了令牌, 我成功地消耗了剩下的 api.
但是当我尝试从 angular 脚本函数请求令牌时,我收到了 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");
});
};
});
我试过你的版本,但它不起作用。
您可以post到url:
http://clientapp:123456@localhost:8080/oauth/token
如果使用 HTTP 身份验证。