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!