如何使用 Facebook 营销替换自定义受众中的​​所有用户 API

How to replace all users in a Custom Audience using the Facebook Marketing API

我正在研究如何替换自定义受众中的​​用户。我可以删除和创建新受众,但理想情况下我只想更新现有受众,因为它与其他帐户共享。

我想我可以使用 create_users_replace 来做到这一点,但我收到错误消息:

facebook_business.exceptions.FacebookRequestError:

  Message: Call was not successful
  Method:  POST
  Path:    https://graph.facebook.com/v13.0/23850060704540982/usersreplace
  Params:  {}

  Status:  400
  Response:
    {
      "error": {
        "message": "(#100) The parameter session is required",
        "type": "OAuthException",
        "code": 100,
        "fbtrace_id": "AOJ9p0Hd1Kla4NRlkhOnHIQ"
      }
    }

这是我尝试使用的代码:

from collections import UserList
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.customaudience import CustomAudience
from facebook_business.api import FacebookAdsApi

test_id = '2385040704549815'

api = FacebookAdsApi.init(access_token=access_token)

session_id = '123456789'
session = {
            'session_id':session_id, 
            'batch_seq': 1, 
            'last_batch_flag':False, 
            }

# List of hashed email addresses (SHA256)
test_audience_list = ["8b84db83027ecd2764ac56dd6ed62aa761ea315e0268c64e34104a6536f"]

# I can add a list of users to a custom audience using this
CustomAudience(test_id).add_users(schema="EMAIL_SHA256", users=test_audience_list)

# I'm unable to replace all users with a new list
CustomAudience(test_id).create_users_replace(fields=None, params=None, batch=None)

我也试过包括会话参数:

CustomAudience(test_id).create_users_replace(fields=None, params=None, batch=None, success=None, failure=None, session=session)

但随后我收到有关意外关键字参数的错误 'session'。

是否可以使用新列表替换自定义受众中的​​所有用户?执行此操作的最佳方法是什么?

这个对我有用:

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.customaudience import CustomAudience

from random import randint

email_sha265 = '<sha265>'
list_id = '<list_id>'

client = FacebookAdsApi.init(
                _app_id,
                _app_secret,
                _access_token
              )
audience = CustomAudience(list_id)

session_id = randint(1000000, 9999999)

params = {
    "session": {
        "session_id": session_id,
        "batch_seq":1,
        "last_batch_flag": "false"
    },
    "payload": { 
        "schema":"EMAIL_SHA256", 
        "data":
        [
          email_sha265
        ]
    }
}

# make the call
audience.create_users_replace(params=params)

回复:

<CustomAudience> {
    "audience_id": "<list_id>",
    "invalid_entry_samples": {},
    "num_invalid_entries": 0,
    "num_received": 1,
    "session_id": "4847542"
}

查看此源代码以获取更多信息:

https://github.com/facebook/facebook-python-business-sdk/blob/main/facebook_business/adobjects/customaudience.py

以及 FB 文档:

https://developers.facebook.com/docs/marketing-api/audiences/guides/custom-audiences/#replace-api