我正在尝试使用Facebook Marketing API在Node.js中制作新的广告系列。
我已成功从此网址获取访问令牌:
var fbApi = {
'method': 'GET',
'url' : 'https://graph.facebook.com/oauth/access_token?client_id=MYCLIENTID&client_secret=MYSECRET&grant_type=client_credentials'
}
然后我发出此POST请求(使用'请求'模块)。 我发布了' name'和' campaign_group_status'我理解的是必需的值。
我发布到端点
https://graph.facebook.com/<API_VERSION>/act_<AD_ACCOUNT_ID>/adcampaign_groups
我正在按照文档中的URL传递访问令牌:
http://graph.facebook.com/endpoint?key=value&access_token=app_id|app_secret
完整代码(由虚拟数据替换的敏感信息):
var request = require('request');
var fbOptions = {
'name' : 'test2',
'campaign_group_status' : 'PAUSED'
}
var fbOptionsString = JSON.stringify(fbOptions);
var fbPost = {
'uri' : ' https://graph.facebook.com/v2.3/act_5374356645419299/adcampaign_groups?access_token=56345345453773242|iert_arfwYfwfwxD-pLxHcpASFTNm',
'method': 'POST',
'headers': {
'Content-Type' : 'application/json',
},
'body' : fbOptionsString
}
request(fbPost, function(error, response, body){
console.log('request says ----', body);
})
我看到的错误回复:
request2 says ---- {"error":{"message":"Unsupported post request.","type":"GraphMethodException","code":100}}
在我收到关于许可的错误之前,另一个人说“&#39; name&#39;值是必需的。那些已经解决了。所以我相信我有正确的访问权限,Facebook正在获取请求的正文内容。
非常感谢帮助!
答案 0 :(得分:0)
如果查看documentation示例,他们会在curl中使用-F选项。
-F, --form <name=content>
(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388. This
enables uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an @ sign. To just get the content part from a file, prefix the file name with the symbol <.
The difference between @ and < is then that @ makes a file get attached in the post as a file upload, while the < makes a text field and just get the contents for that text field from a file.
因此您必须从多部分后期发送数据。 请参阅here以了解如何使用node-request。
var request = require('request');
var fbOptions = {
'name' : 'test2',
'campaign_group_status' : 'PAUSED'
}
// var fbOptionsString = JSON.stringify(fbOptions);
var fbPost = {
'uri' : ' https://graph.facebook.com/v2.3/act_5374356645419299/adcampaign_groups?access_token=56345345453773242|iert_arfwYfwfwxD-pLxHcpASFTNm',
'method': 'POST',
'headers': {
'Content-Type' : 'application/json',
},
'formData' : fbOptions
}
request(fbPost, function(error, response, body){
console.log('request says ----', body);
})
此代码示例未经过测试。