2012-11-24 68 views
1

根據問題標題,我試圖發佈到facebook serverside with node.js 不幸的是,我如何做這件事有什麼問題... 我越來越錯誤如何使用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(); 
}); 
+0

由於您使用的是https,也許您需要定義一個'key'和'cert'來配合您的選項?在節點文檔和此[其他SO問題]中檢出[https節](http://nodejs.org/api/https.html#https_https_request_options_callback)(http://stackoverflow.com/questions/5878740/socket-hang -up-當-使用-HTTPS的請求在節點-JS)。 – theabraham

回答

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(); 
}); 
相關問題