我正在使用RESTful
和Laravel (BackEnd)
创建AngularJS (Front End)
API,我想实现OAuth
以保护我的Laravel API。
我已经在我的laravel中安装了OAuth
但是当我从angularjs调用服务器上的请求时,它会给出invalid_request错误。
这是我的代码:
Laravel Side:
Route::post('oauth/access_token', function() {
return Response::json(Authorizer::issueAccessToken());
});
Angular Side:
$http({
url: $url.siteUrl('oauth/access_token?grant_type=password&username=stafftest&password=password'),
method: "post",
headers: {'Content-Type': "application/x-www-form-urlencoded"},
}).then(function(response) {
console.log(response);
}, function(response) {
console.log(response);
});
我得到错误:
{
"error":"invalid_request",
"error_description":"The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the 'grant_type' parameter."
}
任何想法有什么问题?
谢谢.... :)
答案 0 :(得分:1)
最后我得到了解决方案,我刚刚在数据字段$.param
中添加了AngularJS
:
$http({
url: $url.siteUrl('oauth/access_token'),
method: "post",
data: $.param({client_id:"1",client_secret:"12345",grant_type : 'password', username : 'johndoe', password : 'A3ddj3w'}),
headers: {'Content-Type': "application/x-www-form-urlencoded"},
}).then(function(response) {
console.log(response);
}, function(response) {
console.log(response);
});