Javascript 创建Facebook网站时返回JSON错误
我正试图编写一个node.js脚本来创建一组基于URL结构的Facebook受众,但我得到了以下错误,我似乎无法确定我发送的JSON有什么问题: 我返回的错误:Javascript 创建Facebook网站时返回JSON错误,javascript,node.js,json,facebook,facebook-graph-api,Javascript,Node.js,Json,Facebook,Facebook Graph Api,我正试图编写一个node.js脚本来创建一组基于URL结构的Facebook受众,但我得到了以下错误,我似乎无法确定我发送的JSON有什么问题: 我返回的错误:facebook请求错误:无效的规则JSON格式:无效的规则JSON格式。 “params”对象的“rule”属性似乎在某种程度上是无效的,但我无法确定它有什么问题。我甚至试着复制,结果也出现了同样的错误。我还将JSON粘贴到其中,编辑器指示有效的JSON,但API响应是相同的 在阅读之后,我尝试了一系列使用单引号和双引号的变体,JSO
facebook请求错误:无效的规则JSON格式:无效的规则JSON格式。
“params”对象的“rule”属性似乎在某种程度上是无效的,但我无法确定它有什么问题。我甚至试着复制,结果也出现了同样的错误。我还将JSON粘贴到其中,编辑器指示有效的JSON,但API响应是相同的
在阅读之后,我尝试了一系列使用单引号和双引号的变体,JSON.stringing整个东西,部分东西,没有任何东西,等等。。。我希望新的眼睛能看到它
我的代码:
"use strict";
const bizSdk = require("facebook-nodejs-business-sdk");
const AdAccount = bizSdk.AdAccount;
const CustomAudience = bizSdk.CustomAudience;
const access_token = "REDACTED";
const app_secret = "REDACTED";
const app_id = "REDACTED";
const id = "act_REDACTED";
const pixelID = "REDACTED";
const api = bizSdk.FacebookAdsApi.init(access_token);
const showDebugingInfo = true; // Setting this to true shows more debugging info.
if (showDebugingInfo) {
api.setDebug(true);
}
const logApiCallResult = (apiCallName, data) => {
console.log(apiCallName);
if (showDebugingInfo) {
console.log("Data:" + JSON.stringify(data));
}
};
let fields, params;
fields = [
];
params = {
"name": "Website - Viewed Product - Corrugated Containers - 180 days",
"rule": {
"inclusions": {
"operator": "or",
"rules": {
"inclusions": {
"operator": "or",
"rules": [
{
"event_sources": [
{
"id": pixelID,
"type": "pixel"
}
],
"retention_seconds": 8400,
"filter": {
"operator": "and",
"filters": [
{
"field": "url",
"operator": "i_contains",
"value": "/products/corrugated-containers"
}
]
}
}
]
}
}
}
},
"retention_days": "180",
"prefill": "1"
};
const customaudiences = (new AdAccount(id)).createCustomAudience(
fields,
params
);
logApiCallResult("customaudiences api call complete.", customaudiences);
看起来我不小心把一个rules对象嵌套在了rules对象中!我修正了这个问题,它在没有抛出错误的情况下创建了受众……现在我无法在Facebook的界面中看到受众定义来检查它是否正确,但这是一个完全不同的主题 我改变了
params = {
"name": "Website - Viewed Product - Corrugated Containers - 180 days",
"rule": {
"inclusions": {
"operator": "or",
"rules": {
"inclusions": {
"operator": "or",
"rules": [
{
"event_sources": [
{
"id": pixelID,
"type": "pixel"
}
],
"retention_seconds": 8400,
"filter": {
"operator": "and",
"filters": [
{
"field": "url",
"operator": "i_contains",
"value": "/products/corrugated-containers"
}
]
}
}
]
}
}
}
},
"retention_days": "180",
"prefill": "1"
};
到
有点晚了,但我有一个类似的问题,即使是官方文件中的例子也不起作用,你的回答帮助了我!我的问题是“event_sources”字段应该是一个数组,如您的示例所示,而官方文档将其显示为JSON对象/字典。如果有人在谷歌上发现这个问题(就我的情况而言),我希望这个评论能有所帮助。
params = {
"name": "Website - Viewed Product - Corrugated Containers - 180 days",
"rule": {
'inclusions': {
'operator': 'or',
'rules': [{
'event_sources': [{
'id': pixelID,
'type': 'pixel'
}],
'retention_seconds': retention_seconds,
'filter': {
'operator': 'and',
'filters': [{
'field': 'url',
'operator': 'i_contains',
'value': '/products/corrugated-containers'
}]
}
}]
}
},
"retention_days": retention_days,
"prefill": "1"
};