简体   繁体   English

从Node.js / Javascript连接到OAuth2 API时出现问题?

[英]Issues connecting to OAuth2 API from Node.js/Javascript?

I am trying to connect to the Freshbooks API using OAuth2 and I'm not sure why it is not working. 我正在尝试使用OAuth2连接到Freshbooks API,但不确定为什么它不起作用。 https://www.freshbooks.com/api/authentication https://www.freshbooks.com/api/authentication

I started using the simple-oauth2 library: https://github.com/lelylan/simple-oauth2 so I created the following in my app.js: 我开始使用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');
    }
});

Now I have a button which when pressed calls the /auth route. 现在,我有一个按钮,按下该按钮时会调用/ auth路由。 This opens up the Freshbooks login page, however, once I enter my credentials and click sign in nothing happens, the form stays open and I receive no response back to my app. 这将打开Freshbooks登录页面,但是,一旦我输入我的凭据并单击“登录”,则什么也没有发生,该表单保持打开状态,并且我没有收到对我的应用的任何回复。

Am I missing something? 我想念什么吗? What should I be expecting to happen? 我应该期待什么? Is this an issue with Freshbooks rather than my code? 这是Freshbooks而不是我的代码的问题吗?

Is there a better way to do this rather than using the simple-oauth2 library? 有没有比使用simple-oauth2库更好的方法呢?

Thanks for the help! 谢谢您的帮助!

Are you using localhost in redirect uri? 您是否在重定向uri中使用localhost? While testing I would suggest to use ngrok to generate live https url. 在测试时,我建议使用ngrok生成实时https url。 Set the redirect uri with this host and your callback route. 使用此主机和您的回调路由设置重定向uri。

Also authorizePath needs to be relative route rather than the absolute path. 同样,authorizePath需要是相对路径,而不是绝对路径。 Try using code below: 尝试使用以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM