在要求OAuth2令牌

时间:2015-07-02 20:09:57

标签: angularjs node.js oauth-2.0 authorization restify

我有一个简单的 node.js 后端构建于 Restify ,并且实施了 OAuth2 授权(restify-oauth2插件)。

我在请求令牌时遇到问题。当我使用我的REST客户端调用POST方法时,一切正常,我得到access_tokentoken_type

当我尝试使用 Restangular Angular 中创建的前端应用程序中执行相同操作时,会出现问题。它在发送实际 POST 之前调用 OPTIONS 方法,服务器响应错误:

XMLHttpRequest cannot load http://localhost:8080/token?grant_type=client_credentials.
Invalid HTTP status code 405

以下是我在后端的CORS设置:

server.pre(
function crossOrigin(req,res,next){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
    return next();
  }
);
server.use(restify.fullResponse());

这是前端使用的REST调用:

Restangular.one('token').post(
  null, 
  null, 
  {grant_type: "client_credentials"}, 
  {Authorization: "Basic bXlsb2dpbjpteXBhc3M="}
)

有人可以帮我弄清楚如何让它发挥作用吗?

0 个答案:

没有答案