3

I am attempting to add users to a Facebook Custom Audience using version 2.1 of their API.

I'm using this Python script to create a new audience, and add users to it.

import json, requests

# set up params
account_id = 'an ads account ID'
audience_name = 'a new name'
token = 'a valid access token'

# create a new audience ID for posterity
url =  "https://graph.facebook.com/act_" + account_id + "/customaudiences"
response = requests.post(url, {"name": audience_name, "access_token": token})
resp_dict = json.loads(response.content)
audience_id = resp_dict["id"]

# output new audience name and ID
print("For audience name of:", audience_name)
print("Created audience ID of:", audience_id)

# add email addresses to the audience
the_file = open("the.payload", "r")
payload = the_file.read()
the_file.close()

url = 'https://graph.facebook.com/v2.1/' + audience_id + '/users'
params = {"access_token": token, "payload": payload}
response = requests.post(url, params=params)

# output results
print("Response status code:", response.status_code)
print("Response content:", response.content)

A sample of the contents of "the.payload" is:

{"data":["a8649fb702fb0a67e21ed5120a589cf4d15dd59e2eebb1ad606485731b124100","4842b7883df3c9048abbff1fddb3fd634bed474450f8b2b9102c4bf76fc33381"],"schema":"EMAIL_SHA256"}

Except, in my file, instead of having just 2 valid email addresses that have been formatted as SHA256, and written per hex, as per their docs, I have 1100 of them.

When I run this script, I receive:

('For audience name of:', 'the name I gave')
('Created audience ID of:', u'a valid ID number')
('Response status code:', 200)
('Response content:', '{"audience_id":"a valid ID number","num_received":1100,"num_invalid_entries":0,"invalid_entry_samples":[]}')

However, more than an hour later, "Not Ready, File not uploaded" shows in the UI for 100% of uploads done using this method. enter image description here

Can anyone tell me how to correct my code to add users to custom audiences successfully? I've reviewed Facebook's docs extensively, but I believe I'm following their format.

demonplus
  • 5,613
  • 12
  • 49
  • 68
Becca Petrin
  • 1,494
  • 14
  • 13

1 Answers1

4

This turned out to be an issue with my hashing algorithm.

After posting this, I received a copy of an email hashing algorithm that was proven to work. It was this:

import hashlib
email = "somebody@somewhere.com"
email = email.strip().lower()
email = hashlib.sha256(email).hexdigest()

I compared the hash produced here to my own hash, and it wasn't coming out the same. After correcting it by stripping whitespace and not reusing a hasher in a loop, this problem resolved.

So, Not Ready can result from a hashing issue behind the scenes, or an upload containing too few valid & matching email addresses.

Becca Petrin
  • 1,494
  • 14
  • 13