我试图通过来自Twitch API的POST请求获取访问令牌,但我的http.request
永远不会执行(我不会想到),我无法弄清楚为什么。
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var fs = require('fs');
var https = require('https');
var querystring = require('querystring');
var authInfo = require('./authInfo.json');
app.get('/twitch/auth', function(req, res) {
res.send("auth page");
var data = querystring.stringify({
client_id: authInfo.clientID,
client_secret: authInfo.clientSecret,
grant_type: "authorization_code",
redirect_uri: authInfo.redirectURI,
code: req.query.code,
state: 12345
});
var options = {
host: 'api.twitch.tv',
port: 443,
path: '/kraken/oauth2/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
}
var req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
console.log(d);
});
});
req.on('error', (e) => {
console.log(e);
});
req.end();
});
app.listen(port, function() {
console.log('Point browser to: http://localhost:' + port);
});
PS我为了简洁而省略了一些代码,但我成功地获得了授权码。
答案 0 :(得分:0)
我解决了我的问题。我没有写data
给身体。
...
req.write(data);
req.end();
});
...