javascript - 调用路由后应用程序挂起?
问题描述
我正在尝试构建一个插件以通过他们的 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);
});
谢谢您的帮助!
解决方案
推荐阅读
- python-3.x - 类属性如何在任意数量的参数上具有函数和 getter 设置器?
- javascript - 为什么服务器端在运行 JSONP 跨域 demo 时没有返回正确的函数调用语句?
- svelte - 将值从布局传递到苗条工兵中的嵌套子路由?
- python - 使用 ORM 提取每个组中的第一个条目
- python - Python re.sub 将部分字符串更改为 ascii
- javascript - 使用 *ngFor 并从 ion-label 检索 html 文本
- python - 集合类型的字符串插值
- linux - 如何对齐 Linux 块设备中的缓冲区地址
- javascript - Javascript——在迭代时操作多维数组中的值
- org-mode - 无法将组织模式导出到可折叠 HTML