最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Passport LinkedIn LinkedIn策略的CORS错误

IT培训 admin 5浏览 0评论

Passport LinkedIn LinkedIn策略的CORS错误

所以我正在使用passport.jspassport-linkedin-oauth2策略通过链接输入进行登录。

但是我一直遇到这个错误:

Access to XMLHttpRequest at ';redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fauth%2Fgithub%2Fcallback&client_id=Iv1.56db9f8a973882db' (redirected from 'http://localhost:3000/api/auth/github/') from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

现在,这是我的passport-config.js文件的样子:

var LinkedInStrategy = require('@sokratis/passport-linkedin-oauth2').Strategy;

module.exports = function(passport) {
passport.use(new LinkedInStrategy({
    clientID: 'some client id',
    clientSecret: 'some client secret',
    callbackURL: "http://localhost:3000/api/auth/linkedin/callback",
    profileFields: [ 'id', 'email-address'],
  }, function(accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function () {
      console.log(JSON.stringify(profile))
      return done(null, profile);
    });
  }));
}

我的router.js文件如下所示:


// imports and other code

router.get('/linkedin',
  passport.authenticate('linkedin', { scope: ['r_emailaddress', 'r_liteprofile', ''] }))

router.get('/linkedin/callback', passport.authenticate('linkedin', {
  successRedirect: '/',
  failureRedirect: '/login'
}));

module.exports = router

在我的app.js中,我写了:

// all the above code
var auth = require('./router.js')
app.use('/api/auth', auth)

现在,在我的linkedin开发人员控制台中,我有

  1. 重定向URL: http://localhost:3000/api/auth/linkedin/callback
  2. 域: http://localhost:3000http:localhost:3000/api/auth/linkedin

有人可以告诉我我要去哪里了吗?我知道当路由未列入白名单时会发生CORS错误,但我确实将这些列入白名单。

谢谢。

回答如下:

将以下代码添加到您正在使用所有app.use(...)的index.js中:

app.all('/*', function(req, res) {
    res.header("Access-Control-Allow-Origin", "*");
});

Passport LinkedIn LinkedIn策略的CORS错误

所以我正在使用passport.jspassport-linkedin-oauth2策略通过链接输入进行登录。

但是我一直遇到这个错误:

Access to XMLHttpRequest at ';redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fauth%2Fgithub%2Fcallback&client_id=Iv1.56db9f8a973882db' (redirected from 'http://localhost:3000/api/auth/github/') from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

现在,这是我的passport-config.js文件的样子:

var LinkedInStrategy = require('@sokratis/passport-linkedin-oauth2').Strategy;

module.exports = function(passport) {
passport.use(new LinkedInStrategy({
    clientID: 'some client id',
    clientSecret: 'some client secret',
    callbackURL: "http://localhost:3000/api/auth/linkedin/callback",
    profileFields: [ 'id', 'email-address'],
  }, function(accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function () {
      console.log(JSON.stringify(profile))
      return done(null, profile);
    });
  }));
}

我的router.js文件如下所示:


// imports and other code

router.get('/linkedin',
  passport.authenticate('linkedin', { scope: ['r_emailaddress', 'r_liteprofile', ''] }))

router.get('/linkedin/callback', passport.authenticate('linkedin', {
  successRedirect: '/',
  failureRedirect: '/login'
}));

module.exports = router

在我的app.js中,我写了:

// all the above code
var auth = require('./router.js')
app.use('/api/auth', auth)

现在,在我的linkedin开发人员控制台中,我有

  1. 重定向URL: http://localhost:3000/api/auth/linkedin/callback
  2. 域: http://localhost:3000http:localhost:3000/api/auth/linkedin

有人可以告诉我我要去哪里了吗?我知道当路由未列入白名单时会发生CORS错误,但我确实将这些列入白名单。

谢谢。

回答如下:

将以下代码添加到您正在使用所有app.use(...)的index.js中:

app.all('/*', function(req, res) {
    res.header("Access-Control-Allow-Origin", "*");
});
发布评论

评论列表 (0)

  1. 暂无评论