|
4 | 4 | from twitter_ads.http import Request |
5 | 5 |
|
6 | 6 |
|
7 | | -CONSUMER_KEY = '' |
8 | | -CONSUMER_SECRET = '' |
9 | | -ACCESS_TOKEN = '' |
10 | | -ACCESS_TOKEN_SECRET = '' |
11 | | -ACCOUNT_ID = '' |
| 7 | +CONSUMER_KEY = 'your consumer key' |
| 8 | +CONSUMER_SECRET = 'your consumer secret' |
| 9 | +ACCESS_TOKEN = 'user access token' |
| 10 | +ACCESS_TOKEN_SECRET = 'user access token secret' |
| 11 | +ACCOUNT_ID = 'ads account id' |
12 | 12 |
|
13 | 13 | # initialize the client |
14 | 14 | client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) |
|
17 | 17 | account = client.accounts(ACCOUNT_ID) |
18 | 18 |
|
19 | 19 | # upload an image to POST media/upload |
| 20 | +# https://developer.twitter.com/en/docs/ads/creatives/guides/media-library |
20 | 21 | resource = '/1.1/media/upload.json' |
| 22 | +params = { |
| 23 | + 'additional_owners': '756201191646691328', |
| 24 | + 'media_category': MEDIA_CATEGORY.TWEET_IMAGE |
| 25 | +} |
21 | 26 | domain = 'https://upload.twitter.com' |
22 | 27 | files = {'media': (None, open('/path/to/file.jpg', 'rb'))} |
23 | | -response = Request(client, 'post', resource, files=files, domain=domain).perform() |
24 | | -media_id = response.body['media_id'] |
| 28 | +response = Request(client, 'post', resource, files=files, domain=domain, params=params).perform() |
| 29 | +media_key = response.body['media_key'] |
25 | 30 |
|
26 | 31 | # add to media library |
27 | 32 | media_library = MediaLibrary(account) |
28 | 33 | media_library.name = 'name' |
29 | | -media_library.file_name = 'name.jpeg' |
30 | | -media_library.media_id = media_id |
31 | | -media_library.media_category = MEDIA_CATEGORY.TWEET_IMAGE |
32 | | -media_library.save() |
| 34 | +media_library.file_name = 'name.png' |
| 35 | +media_library.media_key = media_key |
| 36 | +data = media_library.add() |
| 37 | + |
| 38 | +# update the media |
| 39 | +data.name = 'name - updated' |
| 40 | +data.update() |
0 commit comments