Removing User from Tailored Audience

tailored-audiences
restapi
python
delete

#1

With the deprecation of the global opt out audience I wrote a script to remove a list of users from a tailored audience. I’m going to use this to iterate through every audience in my account to remove GDPR use cases. However, the following code isn’t working. Any suggestions how to fix it? The call returns no result. I think I might not be using the payload correctly, but haven’t used it for Twitter before so not sure if it’s correct or not. The credentials work fine as I’m using them to retrieve a list of all tailored audiences in my account, as well as create new audiences.

import requests
from requests_oauthlib import OAuth1
import json

ConsumerKey =‘XXXXXXXXXXXXXXXXXXXXXX’
ConsumerSecret = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
AccessToken= ‘XXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXX’
AccessTokenSecret = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
client = OAuth1(ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret)

twitterID = ‘2p5q2’

url = ‘https://ads-api.twitter.com/5/accounts/18de52ekpx9/tailored_audiences/{0}/users’.format(twitterID)
payload = [
{
“operation_type”: “Delete”,
“params”: {
“users”: [
{
“email”: [“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”]
}
]
}
}
]

response = requests.post(url, auth=client, data=payload)
data = response.content
data = data.decode(‘utf-8’)
print(data)


#2

@eaglennsworld

{
“email”: [‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’]
}

You should use double quote rather than single quote. It’s RFC violation.


#3

Thanks @jrsyo , changed to double quotes. It didn’t resolve the issue, but thanks for pointing that out.


#4

@eaglennsworld

Please debug your response object and provide us response code and response text body.


#5

@jrsyo running the code with the change to double quotes still doesn’t yield any response when making the call (it returns a blank result).


#6

@eaglennsworld
Try print(response) and see what’s in there.


#7

@jrsyo, thanks for all your help. I received a 415 error, which sounds like it might need a header. I have added one, and now running it I get the following error:

‘Response [403]’
b’{“errors”:[{“code”:87,“message”:“Client is not permitted to perform this action.”}]}’

Updated call:
import requests
from requests_oauthlib import OAuth1
import json

ConsumerKey =‘XXXXXXXXXXXXXXXXXXXXXX’
ConsumerSecret = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
AccessToken= ‘XXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXX’
AccessTokenSecret = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
client = OAuth1(ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret)

url = ‘https://ads-api.twitter.com/5/accounts/18de52ekpx9/tailored_audiences/2p5q2/users

headers = {“Content-Type” : “application/json”}
payload = [
{
“operation_type” : “Delete”,
“params” : {
“users” : [
{
“email” : [“4798b8bbdcf6f2a52e527f46a3d7a7c9aefb541afda03af79c74809ecc6376f3”]
}
]
}
}
]

response = requests.post(url, auth=client, headers=headers, json=payload)
print(response)
data = response.content
print(data)


#8

This is likely because your app doesn’t have access to the Audience API. Please reach out to your Twitter representative for assistance.

Thanks!


#10

@imit8me, thanks the app needed to be re-whitelisted. That resolved the issue.