我正在尝试使用 2.1 版 API 将用户添加到 Facebook 自定义受众。
我正在使用此 Python 脚本创建新受众,并向其添加用户。
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)
“the.payload”的内容示例是:
{"data":["a8649fb702fb0a67e21ed5120a589cf4d15dd59e2eebb1ad606485731b124100","4842b7883df3c9048abbff1fddb3fd634bed474450f8b2b9102c4bf76fc33381"],"schema":"EMAIL_SHA256"}
除了在我的文件中,根据他们的文档,我有 1100 个,而不是只有 2 个格式为 SHA256 的有效电子邮件地址,并按照十六进制编写。
当我运行这个脚本时,我收到:
('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":[]}')
但是,一个多小时后,“未就绪,文件未上传”显示在用户界面中,因为 100% 使用此方法完成的上传。
谁能告诉我如何更正我的代码以成功将用户添加到自定义受众?我广泛地查看了 Facebook 的文档,但我相信我遵循的是它们的格式。
最佳答案
事实证明这是我的哈希算法的问题。
发布这篇文章后,我收到了一份电子邮件散列算法的副本,该算法已被证明有效。是这样的:
import hashlib
email = "somebody@somewhere.com"
email = email.strip().lower()
email = hashlib.sha256(email).hexdigest()
我将此处生成的哈希值与我自己的哈希值进行了比较,结果并不相同。通过去除空格而不是在循环中重复使用哈希器来更正它后,这个问题就解决了。
因此,“未就绪”可能是由于幕后的哈希问题,或者包含太少有效且匹配的电子邮件地址的上传。
关于python - 未就绪 : File Not Uploaded when adding to Facebook Custom Audience,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26393962/