|
1 | 1 | from twitter_ads.client import Client
|
2 | 2 | from twitter_ads.creative import Card
|
3 |
| -from twitter_ads.campaign import Tweet |
4 |
| -from twitter_ads.restapi import UserIdLookup |
| 3 | +from twitter_ads.http import Request |
5 | 4 |
|
6 | 5 |
|
7 | 6 | CONSUMER_KEY = 'your consumer key'
|
8 | 7 | CONSUMER_SECRET = 'your consumer secret'
|
9 |
| -ACCESS_TOKEN = 'access token' |
10 |
| -ACCESS_TOKEN_SECRET = 'access token secret' |
11 |
| -ACCOUNT_ID = 'account id' |
| 8 | +ACCESS_TOKEN = 'user access token' |
| 9 | +ACCESS_TOKEN_SECRET = 'user access token secret' |
| 10 | +ACCOUNT_ID = 'ads account id' |
12 | 11 |
|
13 | 12 | # initialize the client
|
14 | 13 | client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
|
15 | 14 |
|
16 | 15 | # load the advertiser account instance
|
17 | 16 | account = client.accounts(ACCOUNT_ID)
|
18 | 17 |
|
19 |
| -# create the card |
20 |
| -name = 'video website card' |
21 |
| -components = [{"type":"MEDIA","media_key":"13_1191948012077092867"},{"type":"DETAILS","title":"Twitter","destination":{"type":"WEBSITE", "url":"http://twitter.com/"}}] |
22 |
| -video_website_card = Card.create(account, name=name, components=components) |
| 18 | +# fetch all |
| 19 | +card = Card.all(account, card_ids="1502039998987587584").first |
| 20 | + |
| 21 | +# fetch by card-id |
| 22 | +card = Card.load(account=account, id="1502039998987587584") |
| 23 | + |
| 24 | +# edit card destination.url |
| 25 | +card.components= [ |
| 26 | + { |
| 27 | + "media_key": "13_794652834998325248", |
| 28 | + "media_metadata": { |
| 29 | + "13_794652834998325248": { |
| 30 | + "type": "VIDEO", |
| 31 | + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", |
| 32 | + "width": 640, |
| 33 | + "height": 360, |
| 34 | + "video_duration": 7967, |
| 35 | + "video_aspect_ratio": "16:9" |
| 36 | + } |
| 37 | + }, |
| 38 | + "type": "MEDIA" |
| 39 | + }, |
| 40 | + { |
| 41 | + "title": "Twitter", |
| 42 | + "destination": { |
| 43 | + "url": "http://twitter.com/newvalue", |
| 44 | + "type": "WEBSITE" |
| 45 | + }, |
| 46 | + "type": "DETAILS" |
| 47 | + } |
| 48 | + ] |
| 49 | + |
| 50 | +card.save() |
| 51 | +print(card.components) |
| 52 | + |
| 53 | +# create new card |
| 54 | +newcard = Card(account=account) |
| 55 | +newcard.name="my new card" |
| 56 | +components= [ |
| 57 | + { |
| 58 | + "media_key": "13_794652834998325248", |
| 59 | + "media_metadata": { |
| 60 | + "13_794652834998325248": { |
| 61 | + "type": "VIDEO", |
| 62 | + "url": "https://video.twimg.com/amplify_video/794652834998325248/vid/640x360/pUgE2UKcfPwF_5Uh.mp4", |
| 63 | + "width": 640, |
| 64 | + "height": 360, |
| 65 | + "video_duration": 7967, |
| 66 | + "video_aspect_ratio": "16:9" |
| 67 | + } |
| 68 | + }, |
| 69 | + "type": "MEDIA" |
| 70 | + }, |
| 71 | + { |
| 72 | + "title": "Twitter", |
| 73 | + "destination": { |
| 74 | + "url": "http://twitter.com/login", |
| 75 | + "type": "WEBSITE" |
| 76 | + }, |
| 77 | + "type": "DETAILS" |
| 78 | + } |
| 79 | + ] |
| 80 | +newcard.components=components |
| 81 | +newcard.save() |
| 82 | +print(newcard.id) |
23 | 83 |
|
24 | 84 | # get user_id for as_user_id parameter
|
25 | 85 | user_id = UserIdLookup.load(account, screen_name='your_twitter_handle_name').id
|
26 | 86 |
|
27 | 87 | # create a tweet using this new card
|
28 |
| -Tweet.create(account, text='Created from the SDK', as_user_id=user_id, card_uri=video_website_card.card_uri) |
| 88 | +Tweet.create(account, text='Created from the SDK', as_user_id=user_id, card_uri=card.card_uri) |
29 | 89 | # https://twitter.com/apimctestface/status/1372283476615958529
|
0 commit comments