如何通过 Facebook API 列出自定义受众?

问题描述

我可以使用 Facebook 图形 API 创建受众,但有没有办法在不知道其 ID 的情况下列出 audiences

import json,requests
response = requests.get(
    f"https://graph.facebook.com/v9.0/search?access_token={FB_TOKEN}",#&fields={fields}",params=f"ad_account_id={AD_ACCOUNT_ID}",timeout=30
)
print(response.text)
print(json.dumps(response.json(),indent=4,sort_keys=False))

只让我知道:

<Response [400]>
{
    "error": {
        "message": "(#27) This method is only available to Workplace apps.","type": "OAuthException","code": 27,"fbtrace_id": "xxx"
    }
}

解决方法

感谢misorude's comment,这就是答案:

import json,requests


fields = "id,name,description"

response = requests.get(
    f"https://graph.facebook.com/v9.0/{AD_ACCOUNT_ID}/customaudiences?access_token={FB_TOKEN}&fields={fields}",timeout=30
)

print(json.dumps(response.json(),indent=4,sort_keys=False))

我还必须使用 f"https://graph.facebook.com/v9.0/me/adaccounts?access_token={FB_TOKEN}&fields={fields}", 从令牌中获取 AD_ACCOUNT_ID 以查看它应该以“act_”开头以避免出现此错误消息:

<Response [400]>
{
    "error": {
        "message": "Unsupported get request. Object with ID 'xxx' does not exist,cannot be loaded due to missing permissions,or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api","type": "GraphMethodException","code": 100,"error_subcode": 33,"fbtrace_id": "xxx"
    }
}