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