1

根据问题标题,我正在尝试使用 node.js 发布到 facebook 服务器端不幸的是,我的操作方式有问题......我收到了错误

{ [错误:套接字挂起]代码:'ECONNRESET'}

app.post('/post/:id?', function(req, res)
{
var id = req.route.params.id;
var token = tokens[id].token;
var path = '/' + id + '/feed?access_token=' + token;
var message = "server side post to facebook";

console.log("post.id = " + req.route.params.id);

var jsonobject = JSON.stringify(
{
    'message'   :   message
});

var options = {
    host: 'graph.facebook.com',
    port: 443,
    path: path,
    method: 'post',
    headers: {
      'content-type': 'application/json',
      'content-length': jsonobject.length()
    }
};

var req = https.request(options, function(res) {
    console.log("statuscode: ", res.statuscode);
    console.log("headers: ", res.headers);
    res.setencoding('utf8');
    res.on('data', function(d) {
        process.stdout.write(d);
    });
    res.on('end', function(){ // see http nodejs documentation to see end
        console.log("finished posting message");
    });
});

req.on('error', function(e) {
    console.error(e);
});

req.write(jsonobject);
req.end();
});
4

1 回答 1

4

我不确定我到底做了什么,但经过大量黑客攻击后它似乎工作......所以对于任何感兴趣的人:

app.post('/post/:id?', function(req, res)
{
var id = req.route.params.id;
var token = tokens[id].token;
var path = '/' + id + '/feed?access_token=' + token;
var strToPost = "server side post to facebook";

console.log("post.id = " + req.route.params.id);

var post_data = querystring.stringify({
    'message' : 'testing server side post'
});

var options = {
    host: 'graph.facebook.com',
    port: 443,
    path: path,
    method: 'POST',
    headers: {
      'Content-Type'    : 'application/x-www-form-urlencoded',
      'Content-Length'  : post_data.length
    }
};

var req = https.request(options, function(res) {
    console.log("statuscode: ", res.statuscode);
    console.log("headers: ", res.headers);
    res.setEncoding('utf8');
    res.on('data', function(d) {
        console.log("res.on data");
        process.stdout.write(d);
    });
    res.on('end', function(){ // see http nodejs documentation to see end
        console.log("\nfinished posting message");
    });
});

req.on('error', function(e) {
    console.log("\nProblem with facebook post request");
    console.error(e);
});

req.write(post_data);
req.end();
});
于 2012-11-25T05:04:39.633 回答