角/节点/ EX preSS /护照 - 连接至Facebook时出现问题(CORS) [英] Angular/Node/Express/Passport - Issues when connecting to facebook(CORS)

查看:150
本文介绍了角/节点/ EX preSS /护照 - 连接至Facebook时出现问题(CORS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要我的NodeJS服务器上使用Facebook的身份验证。我来到 http://scotch.io/ 的教程,并把它使用REST客户端时,在本地工作(邮差等)。

i'm trying to use facebook authentication on my NodeJs server. i came to the tutorial of http://scotch.io/ and have it working locally when using a REST client(Postman, etc).

要改善UI,我用角JS作为前端。但是当我叫http.get我在的NodeJS后端给出了具体的路线我得到丁目和Firefox以下错误:

To improve the UI i used Angular JS as a front end. but when i call a http.get to my specific route given on the NodeJs back-end i get the following error in chome and firefox:

XMLHtt prequest无法加载的https://www.facebook.com/dialog/oauth?response_type=$c$c&redirect_uri=http%…0%2Fapi%2Fauth%2Ffacebook%2Fcallback&scope=email&client_id=3000000000006.无访问控制允许来源标头的请求的资源present。原产地的'localhost:8080',因此是不允许访问

XMLHttpRequest cannot load https://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%…0%2Fapi%2Fauth%2Ffacebook%2Fcallback&scope=email&client_id=3000000000006. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:8080' is therefore not allowed access.

我试过在计算器上几种解决方案,但它仍然没有工作...
我使用的NodeJS前preSS 4. *和CORS中间件。
对角我发现几种方式来实现CORS,没有到目前为止的工​​作。

i've tried several solutions on StackOverflow but its still not working... my NodeJs uses Express 4.* and a CORS middleware. for Angular i found several ways to enable CORS, none working so far.

我希望你能帮帮我!:D在此先感谢

I hope you can help me:D Thanks in advance!

推荐答案

我用角与节点/ EX preSS为好,下面的函数适用于我用我自己的后端服务(我目前不使用它与Facebook是诚实的),所以只检查出来:

I'm using Angular with Node/Express as well, and the following function works for me with my own backend services (I'm currently not using it with Facebook to be honest), so just check it out:

var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, Access-Control-Allow-Origin');
    res.header("Access-Control-Max-Age", "86400"); // 24 hours

    // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
        res.send(200);
    }
    else {
        next();
    }
};

然后,你需要使用它锡 app.confiure()方法如下:

// configure Express
app.configure(function() {
   app.use(allowCrossDomain);
    ...
});

要使用它之前所有其他directrives这一点很重要!

It's important to use it BEFORE all other directrives!

这篇关于角/节点/ EX preSS /护照 - 连接至Facebook时出现问题(CORS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆