创建返回JSON错误的Facebook网站受众

时间:2019-01-21 18:29:43

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

我正在尝试编写一个node.js脚本以基于URL结构创建一组Facebook受众,但是出现以下错误,而且我似乎无法识别JSON的问题。 m发送:

我回来的错误:FacebookRequestError: Invalid rule JSON format: Invalid rule JSON format.

似乎“ params”对象的“ rule”属性在某种程度上是无效的,但是我无法确定它出了什么问题。我什至尝试复制their example from the docs,但它给出了相同的错误。我还将JSON粘贴到the API explorer中,并且那里的编辑器指示有效的JSON,但API响应是相同的。

api explorer screenshot

阅读this similar question之后,我尝试了使用单引号和双引号,JSON进行一连串的变体,将整个内容,其中一部分,全部都不包含在内,等等。我希望可以有新鲜的眼睛抓住它。

我的代码:

"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);

1 个答案:

答案 0 :(得分:0)

似乎我不小心以某种方式将规则对象嵌套在规则对象中!我解决了这个问题,并在没有引发错误的情况下创建了受众群体……现在,我无法在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"
};

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"
};