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

1

1

I'm trying to work out how to replace the users in a Custom Audience. I'm able to delete and create a new audience, but ideally I just want to update the existing audience as it's shared with other accounts.

I think I may be able to do this using create_users_replace but Im getting the error message:

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

Here's the code I'm trying to use:

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)

I've also tried including the session parameter:

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

but then I get an error about an unexpected keyword argument 'session'.

Is it possible to replace all users in a Custom Audience using a new list? What would be the best way to do this?

Orville answered 1/3, 2022 at 17:59 Comment(0)
R
1

This one worked for me:

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)

Response:

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

Check this source code for more information:

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

And the FB Docs:

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

Rustyrut answered 30/3, 2022 at 15:26 Comment(2)
Please edit your answer to add information explaining what you changed and why it works. Using external links isn't encouraged, because they can go dead. Remember, answers aren't just trying to be useful to the OP; they're trying to be useful to many other programmers having the same problem. See How to Answer for more information.Astto
Hi! I'm using this API for the same purpose, but I need to replace more than 10K users (fb's api limit per batch). I'm not sure how to achieve this, since the updating process (the call itself) takes a lot. I'm doing multiple calls setting the same session_id and incrementing the batch_seq param, carefully setting last_batch_flag: after the first call I always end up waiting forever for the replace update to finish. Is there a way to set it in the method call? Thank you so much in advance!Acyclic

© 2022 - 2024 — McMap. All rights reserved.