0

我正在尝试使用 OAuth2 连接到 Freshbooks API,但我不确定它为什么不起作用。https://www.freshbooks.com/api/authentication

我开始使用 simple-oauth2 库:https ://github.com/lelylan/simple-oauth2所以我在我的 app.js 中创建了以下内容:

const oauth2 = simpleOauthModule.create({
    client: {
        id: process.env.CLIENT_ID,
        secret: process.env.CLIENT_SECRET,
    },
    auth: {
        tokenHost: 'https://api.freshbooks.com',
        tokenPath: '/auth/oauth/token',
        authorizePath: 'https://my.freshbooks.com/service/auth/integrations/sign_in',
    },
});

//authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
    redirect_uri: 'https://localhost:3000/callback',
    //scope: 
    //state:
});

//initial page redirecting to freshbooks
router.get('/auth', function(req, res) {
    console.log('Inside /auth');
    console.log(authorizationUri);
    res.redirect(authorizationUri);
});

//callback service parsing the aurothization token and asking for access token
router.get('/callback', async (req, res) => {
    console.log('Inside /callback');
    const code = req.query.code;
    const options = {
        code,
    };

    try {
        const result = await oauth2.authorizationCode.getToken(options);
        console.log('The resulting token: ', result);

        return res.status(200).json(token);
    } catch(error) {
        console.error('Access token error', error.message);
        return res.status(500).json('Authentication failed');
    }
});

现在我有一个按钮,按下时会调用 /auth 路由。这将打开 Freshbooks 登录页面,但是,一旦我输入我的凭据并单击登录,没有任何反应,表单保持打开状态,并且我没有收到对我的应用程序的回复。

我错过了什么吗?我应该期待发生什么?这是 Freshbooks 的问题,而不是我的代码的问题吗?

有没有比使用 simple-oauth2 库更好的方法来做到这一点?

谢谢您的帮助!

4

1 回答 1

0

您在重定向 uri 中使用 localhost 吗?在测试时,我建议使用它ngrok来生成实时 https url。使用此主机和您的回调路由设置重定向 uri。

还 authorizePath 需要是相对路径而不是绝对路径。尝试使用下面的代码:

const oauth2 = simpleOauthModule.create({
  client: {
    id: process.env.CLIENT_ID,
    secret: process.env.CLIENT_SECRET,
  },
  auth: {
    tokenHost: 'https://api.freshbooks.com',
    tokenPath: '/auth/oauth/token',
    authorizePath: '/service/auth/integrations/sign_in',
  },
});

//initial page redirecting to freshbooks
router.get('/auth', function(req, res) {
  console.log('Inside /auth');
  const authorizationUri = oauth2.authorizationCode.authorizeURL({
    redirect_uri: 'https://<ngrok_tunnel_id>.ngrok.io/callback'
  });
  console.log(authorizationUri);
  res.redirect(authorizationUri);
});

//callback service parsing the aurothization token and asking for access token
router.get('/callback', async (req, res) => {
  console.log('Inside /callback');
  const code = req.query.code;
  const options = {
    code
  };

  try {
    const result = await oauth2.authorizationCode.getToken(options);
    console.log('The resulting token: ', result);

    return res.status(200).json(token);
  } catch(error) {
    console.error('Access token error', error.message);
    return res.status(500).json('Authentication failed');
  }
});

module.exports = router
于 2018-07-15T04:04:22.903 回答