javascript - getaddrinfo ENOTFOUND https://graph.facebook.com/
问题描述
我正在使用 Nodehttps
库将数据发送到 Facebook API。我最终得到错误:
getaddrinfo ENOTFOUND https://graph.facebook.com/ https://graph.facebook.com/:443
我觉得这很奇怪,因为用 Postman 发出的相同请求没有任何问题。
我试过nslookup
:
$ nslookup https://graph.facebook.com/
Server: 1.1.1.1
Address: 1.1.1.1#53
** server can't find https://graph.facebook.com/: NXDOMAIN
不知道是什么原因造成的。
这是我的带有匿名令牌的代码:
var inputData = new Array;
inputData.firstname = "First name";
inputData.lastname = "Last name"
inputData.phone = "Phone";
inputData.email = "Email";
inputData.workable_stage = "Applied";
inputData.access_token = 'xxx';
var https = require('https');
var crypto = require('crypto'); // Facebook API requires hashing of user data
var querystring = require('querystring');
const firstname_hash = crypto.createHash('sha256');
const lastname_hash = crypto.createHash('sha256');
const phone_hash = crypto.createHash('sha256');
const email_hash = crypto.createHash('sha256');
// hash user data
firstname_hash.update(inputData.firstname);
var firstname_hashed = firstname_hash.digest('hex');
lastname_hash.update(inputData.lastname);
var lastname_hashed = lastname_hash.digest('hex');
phone_hash.update(inputData.phone);
var phone_hashed = phone_hash.digest('hex');
email_hash.update(inputData.email);
var email_hashed = email_hash.digest('hex');
// prepare data object
var fb_data = [
{ match_keys:
{
"fn": firstname_hashed,
"ln": lastname_hashed,
"phone": phone_hashed,
"email": email_hashed
},
event_name: "Lead",
event_time: Date.now() / 1000 | 0, // UNIX timestamp in seconds, see: https://stackoverflow.com/a/221297/6178132
custom_data: {
workable_stage: inputData.workable_stage,
}
}
]
body = {
access_token: inputData.access_token,
upload_tag: "store_data",
data: fb_data
}
console.log(querystring.stringify(body));
var postData = querystring.stringify(body);
// send http request
const options = {
hostname: 'https://graph.facebook.com/',
port: 443,
path: '/v3.3/123/events',
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = https.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
// Write data to request body
req.write(postData);
req.end();
解决方案
如果您像这样更改选项,请求应该可以工作,则主机名参数是唯一需要更新的参数:
const options = {
hostname: 'graph.facebook.com',
port: 443,
path: '/v3.3/123/events',
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
'Content-Length': Buffer.byteLength(postData)
}
};
推荐阅读
- ms-access - 'Id' 不是此表中的索引
- java - 从超类获取浮动属性到超类
- mysql - 如何从数据库中获取数据,在 MATLAB 中,使用“and”运算符的 where 子句?
- java - 如何将文件内容传递给招摇的@ExampleProperty 注释值?
- google-cloud-platform - 如何将 OAuth 应用程序的测试电子邮件帐户列入白名单/授权
- c++ - Qt:如何让 QImage 知道更新的内存缓冲区?
- php - 使用变量访问 PHP 对象数组
- tcpdump - 如何在 tcpdump 中每个捕获的数据包之间设置分隔符?
- asp.net - SSMS 和 ASP.NET 中存储过程的不同输出
- javascript - 如何将图像文件添加到表格单元格,其中这两个元素都是使用 JS 动态创建的?