首页 > 解决方案 > 调用路由后应用程序挂起?

问题描述

我正在尝试构建一个插件以通过他们的 API 连接到我的 Freshbooks 帐户。我已经设法建立了与 API 的连接,但是一旦我提出请求(我可以在控制台中看到“提出请求!”),应用程序就会挂起。我可以在控制台中看到我的请求已发出,但没有任何反应。是不是还在等回复?

这是我尝试使用的代码:

router.get("/", function(req, res) {
    res.render("landing");
});

//setup the parameters for connecting to freshbooks api using simple-oauth2 library
const oauth2 = simpleOauthModule.create({
    client: {
        id: process.env.FRESHBOOKS_CLIENT_ID,
        secret: process.env.FRESHBOOKS_CLIENT_SECRET,
    },
    auth: {
        tokenHost: config.freshbooks.token_host,
        tokenPath: config.freshbooks.toke_path,
        authorizePath: config.freshbooks.authorize_path,
    },
    options: {
        bodyFormat: 'json',
        authorizationMethod: 'body',
    },
});

//authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
    redirect_uri: 'https://localhost/callback'
});

//initial page redirecting to freshbooks
router.get('/auth', function(req, res) {
    //console.log('--DEBUG-- Inside /auth');
    console.log('--DEBUG-- Authorization URI: ' + authorizationUri);
    res.redirect(authorizationUri);
});

//callback service parsing the authorization token and asking for access token
// router.get('/callback', async (req, res) => {
router.get('/callback', function(req, res) {
    //console.log('--DEBUG-- Inside /freshbooks');
    //const code = req.query.code;

    //set the authorization code into the environment variables
    config.freshbooks.auth_code = req.query.code;
    console.log('--DEBUG-- Authorization Code: ' + config.freshbooks.auth_code);

    //set the specific headers required by freshbooks as per documentation
    var headers = {
        'Api-version': 'alpha',
        'Content-Type': 'application/json',
        'Authorization': 'Bearer'
    };

    //set the data needed for getting token from freshbooks api
    var data = {
        grant_type: 'authorization_code',
        client_id: process.env.FRESHBOOKS_CLIENT_ID,
        client_secret: process.env.FRESHBOOKS_CLIENT_SECRET,
        code: config.freshbooks.auth_code,
        redirect_uri: 'https://localhost/callback'
    };

    //set the options for the request to get token
    var options = {
        url: config.freshbooks.token_host + config.freshbooks.token_path, 
        headers: headers, 
        method: 'GET', 
        body: JSON.stringify(data)
    };

    function callback(error, response, body) {
        console.log('--DEBUG-- Made the request!');
        if (!error && !(res.statusCode >= 200 && res.statusCode < 300)) {
            console.log('--DEBUG-- Authorization Token Response: ' + data);

            //set the access and refresh tokens in the environment variables
            var tokens = JSON.parse(body);
            config.freshbooks.access_token = tokens.access_token;
            config.freshbooks.refresh_token = tokens.refresh_token;
        } else {
            error = new Error('--HTTP Error-- Authorization Token: ' + res.statusCode + ': ' + body);
        }
    }

    //request access token from freshbooks api
    request(options, callback);
});

谢谢您的帮助!

标签: javascriptnode.jsasynchronous

解决方案


推荐阅读