我使用oauth2-server-laravel包与angularjs
作为客户端。我已经实现了它的密码方法,适用于GET请求。
当我尝试在安全链接上执行$http.post
时。它在401中给出了以下错误。
{"status":401,"error":"unauthorized","error_message":"Access token is missing"}
POST
请求时才会发生这种情况。 (我尝试将access_token作为post参数和Http标头发送。)postman
时效果很好!oauth
移除route
过滤器时,它在角度上工作正常。 所以这可能是oauth2-server-laravel包或AngularJS 我已将此代码添加到filters.php
以支持CORS。
App::before(function($request)
{
//
if($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
$statusCode = 204;
$headers = [
'Access-Control-Allow-Origin' => 'http://localhost:8100',
'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers' => 'Origin, Content-Type, Accept, Authorization, X-Requested-With',
'Access-Control-Allow-Credentials' => 'true'
];
return Response::make(null, $statusCode, $headers);
}
});
App::after(function($request, $response)
{
//
$response->headers->set('Access-Control-Allow-Origin', 'http://localhost:8100');
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
$response->headers->set('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization, X-Requested-With');
$response->headers->set('Access-Control-Allow-Credentials', 'true');
return $response;
});
JS代码:
//this wprks
$http.get(linkshare.host+'/link?access_token=Mz44ZaWffS9YlrPG08SN8ivLMI0xkCd9bcRL2tny').success(function(data) {
$scope.links = data.links ;
localStorage.setItem("my-local-data", JSON.stringify(data));
});
var postLinkdata = {
"access_token" : "Mz44ZaWffS9YlrPG08SN8ivLMI0xkCd9bcRL2tny",
"title" : "Hello",
"url" : "http://hello.world"
};
// this doesn't work
$http.post(linkshare.host+'/link',postLinkdata).success(function(mydata){
console.log(mydata);
});
答案 0 :(得分:0)
传递GET参数和POST数据的方式不正确。在这种情况下,GET参数应该是config
对象的一部分:
// POST parameters
var postLinkdata = {
title: "Hello",
url : "http://hello.world"
};
// Additional GET parameters
var config = {
params: {access_token: "Mz44ZaWffS9YlrPG08SN8ivLMI0xkCd9bcRL2tny"}
};
$http.post(linkshare.host+'/link', postLinkdata, config).success(function(mydata){
console.log(mydata);
});
请参阅$http.post
的文档。