具有Fusionauth passport-oauth OAuth2Strategy错误的节点。获取访问令牌失败

我试图通过fusionauth用passport和express授权我的node应用,在登录fusionauth后,我从fusionauth回调得到一个节点错误 “Failed to obtain access token”。我不知道为什么fusionauth的响应不包括token?

fusionauth授权链接与回调。fusion_auth_server:9011oauth2authorize?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Foauth2%2Fcallback&client_id=42a5##-##-##-##-##-##-##-##-##-##-##-##-##-##-##。

 name: 'InternalOAuthError',
  message: 'Failed to obtain access token',
  oauthError:
   { Error: connect EHOSTUNREACH 0.0.35.51:80 - Local (192.168.1.46:62475)
       at internalConnect (net.js:872:16)
       at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)
       at GetAddrInfoReqWrap.emitLookup [as callback] (net.js:1019:9)
       at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:61:10)
     errno: 'EHOSTUNREACH',
     code: 'EHOSTUNREACH',
     syscall: 'connect',
     address: '0.0.35.51',
     port: 80 } }

```
app.get('/oauth2/authorize', oauth2.authorize);
app.get('/oauth2/callback', oauth2.callback);
app.get('/oauth2/logout', oauth2.logout);

```

```
const passport = require('passport');
const OAuth2Strategy = require('passport-oauth').OAuth2Strategy;
const http = require('http');
const config = {
    "apiKey": "63353861-####-####-####-##########",
    "callbackURL": "http://localhost:3000/oauth2/callback",
    "clientID": "42a5bc23-####-####-####-#####",
    "clientSecret": "WI2Y04lkozWonBeRz_####################",
    "host": "fusion_auth_server", 
    "port": "9011"
    };

passport.use(
  'fusionauth',
  new OAuth2Strategy(
    {
      authorizationURL: `${config.host}:${config.port}/oauth2/authorize`,
      tokenURL: `${config.host}:${config.port}/oauth2/token`,
      clientID: config.clientID,
      clientSecret: config.clientSecret,
      callbackURL: config.callbackURL
    },
    function(accessToken, refreshToken, profile, done) {
      // verify accessToken was provided`enter code here`
      if (!accessToken) {
        done(null, false);
      }

      // verify token and get user info
      const options = {
        host: config.host,
        port: config.port,
        path: '/oauth2/userinfo',
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
          Authorization: `Bearer ${accessToken}`
        }
      };
      const userInfoRequest = http.get(options, res => {
        var chunks = '';
        res.on('data', data => {
          chunks += data;
        });
        res.on('end', () => {
          if (res.statusCode === 200) {
            const result = JSON.parse(chunks);
            const user = {
              ...result,
              accessToken
            };

            // todo: persist user

            done(null, user);
          } else {
            done(null, false);
          }
        });
      });
      userInfoRequest.end();
    }
  )
);

const callback = (req, res, next) => {
    //console.log("callback",res)
  passport.authenticate('fusionauth', (err, user) => {
     console.log("Authenticating",err)
    if (err) {
      return next(err);
    }
    if (!user) {
      return res.redirect('http://localhost:4200/login');
    }
   // console.log(user);
    res.cookie('accessToken', user.accessToken, { httpOnly: true });
    res.redirect('http://localhost:4200');
  })(req, res, next);
};

module.exports = {

  authorize: passport.authenticate('fusionauth', {
    session: false
  }),
  callback,
  logout: (req, res) => {
    req.logout();
    res.redirect('http://localhost:4200/');
  }
};


```

解决方案:

看起来像是某种网络错误,这是我的第一个猜测。Error: connect EHOSTUNREACH 0.0.35.51:80. 这是一个奇怪的IP地址。

我看到配置的主机是 fusion_auth_server. 是否解析到该IP地址?

给TA打赏
共{{data.count}}人
人已打赏
未分类

按价格过滤,更改按钮标签

2022-9-9 9:43:19

未分类

如何改进这个List Comprehensions?

2022-9-9 9:43:21

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索