PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 3, 2022

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

 November 03, 2022     facebook-graph-api, facebook-marketing-api, facebook-python-business-sdk, python     No comments   

Issue

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?


Solution

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



Answered By - Moritz
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing