It looks like you are missing headers in your request with Content-Length
and Content-Type
:
exports.postSubscription = function(endpointUrl, appId, accessToken, object, fields, callback) {
var postData = querystring.stringify({
"object": object,
"fields": fields,
"callback_url": endpointUrl,
"verify_token": "abc123"
});
var options = {
host:'graph.facebook.com',
port:443,
path:'/'+appId+'/subscriptions?access_token='+accessToken,
method:'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length;
}
};
var req = https.request(options, function(res) {
var data = "";
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
callback(data);
console.log(data);
});
res.on('error', function(e) {
callback(e.message, null);
});
});
req.write(postData);
req.end();
};
On top of this, you need to handle requests from Facebook with updates. If you do not handle them, then you will face errors.