获取"阅读ECONNRESET"尝试POST到' graph.facebook.com'通过node.js http

时间:2016-04-19 17:44:32

标签: javascript node.js facebook http facebook-graph-api

尝试向' graph.facebook.com'做一个POST请求通过node.js http模块,但得到"读取ECONNRESET"每一次。

所有参数都有效 - 通过curl检查相同的请求并且它有效。

var http = require('http');

var token = "...";

sendTextMessage(XXX, "Text received");

function sendTextMessage(sender, text) {
  var messageData = {
    text:text
  };

  var json = {
    recipient: {id:sender},
    message: messageData,
  };

  var body = JSON.stringify(json);

  var options = {
    host: "graph.facebook.com",
    path: '/v2.6/me/messages?access_token=' + token,
    port: 443,
    method: 'POST',
    headers: {'Content-Type': 'application/json',
              'Content-Length': body.length }
  };


  var callback = function(response) {
    console.log('Status: ' + response.statusCode);
    console.log('Headers: ' + JSON.stringify(response.headers));

    var str = ''
    response.on('data', function (chunk) {
      str += chunk;
    });

    response.on('end', function () {
      console.log("end:" + str);
    });
  }

  var req = http.request(options, callback);
  req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
  });
  //This is the data we are posting, it needs to be a string or a buffer
  req.write(body);
  req.end();
}

任何线索?

1 个答案:

答案 0 :(得分:1)

好吧,终于找到了答案

我必须替换

var http = require('http')

var https = require('https')