我正在尝试使用 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 库更好的方法来做到这一点?
谢谢您的帮助!