Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Node.js发布到facebook服务器端_Facebook_Node.js_Facebook Graph Api_Https - Fatal编程技术网

如何使用Node.js发布到facebook服务器端

如何使用Node.js发布到facebook服务器端,facebook,node.js,facebook-graph-api,https,Facebook,Node.js,Facebook Graph Api,Https,根据问题标题,我正试图用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

根据问题标题,我正试图用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();
});

我不确定我到底做了什么,但经过多次黑客攻击后,它似乎起了作用。。。 因此,对于任何感兴趣的人:

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();
});

我不确定我到底做了什么,但经过多次黑客攻击后,它似乎起了作用。。。 因此,对于任何感兴趣的人:

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();
});

由于您使用的是https,您可能需要定义一个
证书
,以配合您的选项?签出节点文档中的。由于您使用的是https,可能需要定义一个
证书
,以配合您的选项?签出节点文档中的和此。