2014-09-04 51 views
0

我正在使用AppGyver和AngularJS构建移动应用程序。请求OAUTH令牌时出现重大错误

Restangular.all("token") 
.post({ 
    grant_type: "password", 
    username:username, 
    password:password 
}); 

我RESTangular是建立这样:

appServerModule.config(function(RestangularProvider) { 

    RestangularProvider.setBaseUrl('http://192.168.1.58/AppServer/api'); 

    RestangularProvider.setErrorInterceptor(function(response) { 

     steroids.logger.log("RestangularProvider error:" + JSON.stringify(response)); 
    }); 
}); 

当我执行POST

在登录页面,我用这个做一个POST请求我的Web API服务器请求我在记录器中获得以下输出:

"RestangularProvider error:{"data":"","status":0,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"},"url":"http://192.168.1.58/AppServer/api/token","data":{"grant_type":"password","username":"AppServer","password":"AppServer"}},"statusText":""}" 

正如您所看到的,错误消息不包含任何有用的信息,所以我不知道发生了什么问题。 我可以在浏览器中使用Postman执行POST,并且Web API设置为允许CORS。

我在这里错过了什么? 如何获得有关幕后发生的更多信息?

请注意,我在移动设备上执行此操作。

额外的信息: GET请求提供这种预期Restangular错误:

"RestangularProvider error:{"data":{"Message":"Authorization has been denied for this request."},"status":401,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"headers":{"Accept":"application/json, text/plain, */*"},"url":"http://192.168.1.58/AppServer/api/chapters"},"statusText":"Unauthorized"}" 

回答

1

OWIN的OAuth 2.0授权服务器不支持JSON的令牌请求。

您需要使用的X WWW的形式,进行了urlencoded,而不是JSON同时使从JavaScript客户端请求:

Restangular.all("token") 
.post("grant_type=password&username=YourUsername&password=YourPassword", undefined, { 
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 
});