How to upload video and get video_id via API


#1

i have found this: https://dev.twitter.com/rest/reference/post/media/upload-init, i can use following python code to upload image

url = "https://upload.twitter.com/1.1/media/upload.json"
headers = "{‘user-agent’: ‘twitter-ads version: 1.2.0 platform: Python 2.7 (CPython/linux2)’}"
local_file_name = "/data09/home/shenweihai/twitter_ads_api/at.jpg"
files = {u’media’: (None, open(local_file_name, “rb”))}
requests.POST(url, headers=headers, files=files)

however, how to upload video.


#2

We have a demo app written in Python that describes how to do chunked uploads for videos.


#3

thank you, it looks like great.


#4

it works. I get media_id from it.
but i get a trouble when i want to associate this video with certain account. https://dev.twitter.com/ads/reference/post/accounts/%3Aaccount_id/videos
it says:

[{u’message’: u’Media referenced by media_id id was not found.’, u’code’: u’INVALID_MEDIA_ID’}]


#5

Hey @TopBuzzApp

Can you give us some additional information regarding your issue?

  • The Ads account in question
  • The full request and response bodies of calls made to the Ads API

In addition, can you clarify the use case here? Are you trying to upload a video to be used in a Pre-roll Views campaign?


#6

hi, i want to use the Ads API to promoted Video through the Tweet(through the Video App Download Card).
There are four steps.

  • I have already finished Step One( with @andypiper’s help, Demo), I can upload the video and get the media_id of The video.

  • Then, in the second Step, i use “Post /videos” to associate the “media_id” with certain account. And i got the err.

request:
twurl -H ads-api.twitter.com -X POST -d “video_media_id=:media_id&title=-&description=-” /1/accounts/:account_id/videos |jq .
response err:

[{u’message’: u’Media referenced by media_id id was not found.’, u’code’: u’INVALID_MEDIA_ID’}]
in addition, i assume that i can get the result from:
twurl -H ‘ads-api.twitter.com’ /1/accounts/18ce54evwnn/videos | jq .

Does a campaign have serveral different types? I even donot notice it.


#7

@andypiper @imit8me I have solved it.

because i have no permission when i associate the Video with certain account, so i fill the “additional_owners” with certain account’s promotable user_id and set “media_category” as amplify_video, it works.


#8

@TopBuzzApp

Glad to hear it! Please consider marking this thread as “Resolved”

Thanks!