简体   繁体   中英

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. 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:

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. 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.

Am I missing something? What should I be expecting to happen? Is this an issue with Freshbooks rather than my code?

Is there a better way to do this rather than using the simple-oauth2 library?

Thanks for the help!

Are you using localhost in redirect uri? While testing I would suggest to use ngrok to generate live https url. Set the redirect uri with this host and your callback route.

Also authorizePath needs to be relative route rather than the absolute path. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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