sam*_*ris 5 node.js trello node-https
我正在尝试使用以下 URL 获取 Trello 板的 JSON 数据,使用 Node.js 的https模块:
这是我的代码:
var https = require('https');
https.get('https://trello.com/b/nC8QJJoZ.json', function (res) {
console.log('statusCode:', res.statusCode);
console.log('headers:');
console.log(res.headers);
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log(chunk);
});
}).on('error', function (e) {
console.log('ERROR: ' + e);
});
Run Code Online (Sandbox Code Playgroud)
尽管 URL 在浏览器中运行良好,但它返回一个包含字符串“无效密钥”的正文,状态为 401。以下是输出:
statusCode: 401
headers:
{ 'cache-control': 'max-age=0, must-revalidate, no-cache, no-store',
'x-content-type-options': 'nosniff',
'strict-transport-security': 'max-age=15768000',
'x-xss-protection': '1; mode=block',
'x-frame-options': 'DENY',
'x-trello-version': '1.430.0',
'x-trello-environment': 'Production',
'set-cookie':
[ 'dsc=ae78a354044f982079cd2b5d8adc4f334cda679656b3539ee0adaaf019aee48e; Path=
'visid_incap_168551=/NYMaLRtR+qQu/H8GYry1BCKl1UAAAAAQUIPAAAAAAC1zWDD1JLPowdC
'incap_ses_218_168551=+/2JSB4Vz0XJO/pWbX4GAxCKl1UAAAAA0pAbbN5Mbs4tFgbYuskVPw
expires: 'Thu, 01 Jan 1970 00:00:00',
'content-type': 'text/plain; charset=utf-8',
'content-length': '12',
etag: 'W/"c-b1ec112"',
vary: 'Accept-Encoding',
date: 'Sat, 04 Jul 2015 07:24:00 GMT',
'x-iinfo': '1-11281210-11279245 PNNN RT(1435994639565 404) q(0 0 0 -1) r(3 3) U
'x-cdn': 'Incapsula' }
invalid key
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
好吧,事实证明,我们需要在请求中提供 Trello API 应用程序密钥(从此处生成)。
var https = require('https');
var KEY = '<replace this with your app key>';
https.get('https://trello.com/b/nC8QJJoZ.json?key=' + KEY, function (res) {
...
});
Run Code Online (Sandbox Code Playgroud)
在我看来,这是一个奇怪的要求,因为我们没有使用 Trello 的API 端点。(即使我解决了问题,我仍然想知道为什么浏览器可以访问资源,但服务器端脚本不能。)
| 归档时间: |
|
| 查看次数: |
2015 次 |
| 最近记录: |