4

I am using following url for fetching users tweets.

https://api.twitter.com/2/users/{user_twitter_id}/tweets

Following are my Request parameters

query_params = {
    'max_results': max_results,
    'expansions': 'attachments.media_keys',
    'tweet.fields': 'id,created_at,text,author_id,in_reply_to_user_id,referenced_tweets,attachments,withheld,geo,entities,public_metrics,possibly_sensitive,source,lang,context_annotations,conversation_id,reply_settings',
    'media.fields': 'media_key,duration_ms,height,preview_image_url,type,url,width,public_metrics,non_public_metrics,organic_metrics,promoted_metrics,alt_text'
}

Below is the response I am getting inside "Includes" which have media list

"includes": {
    "media": [
        {
            "height": 750,
            "media_key": "3_1489397927281840131",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKtnhhDWUAMtyBi.jpg",
            "width": 1125
        },
        {
            "height": 750,
            "media_key": "3_1489397930452783110",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKtnhs3XEAYeMkP.jpg",
            "width": 1125
        },
        {
            "height": 750,
            "media_key": "3_1489397944214302727",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKtnigIXMAcTO6t.jpg",
            "width": 1125
        },
        {
            "duration_ms": 242784,
            "height": 1080,
            "media_key": "13_1489018359819771906",
            "preview_image_url": "https://pbs.twimg.com/media/FKoOePDWYAA1ZZB.jpg",
            "public_metrics": {
                "view_count": 275300
            },
            "type": "video",
            "width": 1920
        },
        {
            "height": 2400,
            "media_key": "3_1488933061307809794",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKnAuwWWQAIZZ8J.jpg",
            "width": 3000
        },
        {
            "height": 2000,
            "media_key": "3_1488640905187938304",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKi3BB_X0AA47_h.jpg",
            "width": 3000
        },
        {
            "duration_ms": 41374,
            "height": 1080,
            "media_key": "13_1488623384250527746",
            "preview_image_url": "https://pbs.twimg.com/amplify_video_thumb/1488623384250527746/img/K2fiO7GwjmxL0H89.jpg",
            "public_metrics": {
                "view_count": 239341
            },
            "type": "video",
            "width": 1080
        },
        {
            "height": 2000,
            "media_key": "3_1488548514921603078",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKhi_NbWYAYqP04.jpg",
            "width": 3000
        },
        {
            "height": 750,
            "media_key": "3_1488336416732028931",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKeiFeGXIAMgHQx.jpg",
            "width": 1125
        },
        {
            "duration_ms": 53136,
            "height": 1080,
            "media_key": "13_1488316251667582978",
            "preview_image_url": "https://pbs.twimg.com/amplify_video_thumb/1488316251667582978/img/DE2q07gtwoARK76r.jpg",
            "public_metrics": {
                "view_count": 214984
            },
            "type": "video",
            "width": 1080
        },
        {
            "duration_ms": 40248,
            "height": 1080,
            "media_key": "13_1488154727544152064",
            "preview_image_url": "https://pbs.twimg.com/media/FKb85iAXoAou4ED.jpg",
            "public_metrics": {
                "view_count": 242329
            },
            "type": "video",
            "width": 1080
        },
        {
            "height": 913,
            "media_key": "3_1487927712761229314",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKYuXxKXsAIbXd2.jpg",
            "width": 1200
        },
        {
            "duration_ms": 35785,
            "height": 1080,
            "media_key": "13_1487538546948939777",
            "preview_image_url": "https://pbs.twimg.com/amplify_video_thumb/1487538546948939777/img/qzUmEZKmD6ii_0dM.jpg",
            "public_metrics": {
                "view_count": 290603
            },
            "type": "video",
            "width": 1080
        }
    ]
}

As you can see in response inside media we have type video and media key, but no actual video url all we can see is video thumbnail. So please tell me how we can fetch video urls along with preview image url

Huwaiza
  • 77
  • 2
  • 13
  • This is correct. Unfortunately at the current time, the video URL is not yet available in v2 API responses. You will need to use v1.1 to retrieve the URL if that data is important to your application. – Andy Piper Feb 04 '22 at 10:02
  • @AndyPiper I have also tried using the v1.1 API This was my request https://api.twitter.com/1.1/statuses/show.json?id=1484173968051425281 This tweet included video as well, but in response i was not able to see any video url, I also tried to give params for media attachments, url, entities it still did not gave me any video url.. please help me out in this one, I have been trying for days – Huwaiza Feb 04 '22 at 11:31
  • Please mark as answered below if this is the correct solution, thanks. – Andy Piper Feb 04 '22 at 13:36

1 Answers1

5

There are a couple of things to note here:

  • in Twitter API v2, at the time of writing this answer in Feb 2022, the video URL is not currently available. This is a known omission that Twitter is working on.

  • in Twitter API v1.1, you generally should be able to get the video URLs. There are two additional things to know about...

    • if the Tweet is a longer (more than 140 characters) Tweet, then you will need to add tweet_mode=extended to your API call to ensure that the whole Tweet data and extended entities are included. You can tell if you have not retrieved the whole Tweet if the truncated field value is true. This is the case in your example Tweet ID above. Using tweet_mode=extended you will see the video URLs.
    • if the Tweet is from the Ads platform, the video may be part of a Ads card format, and these are not served via the API. You can tell this by checking the source field for evidence that the Tweet was posted as an ad. The advertiser is able to mark the media as not for wider syndication / API access in that case, and the video URL is not returned in the API.
Andy Piper
  • 11,422
  • 2
  • 26
  • 49
  • 1
    Well thanks, I did have to use API v1.1 and it worked fine for me.. but I want to mention one more thing here if there was link attached to tweet and that link was video, it won't then give us direct video link. It will only add video link if the video or image was uploaded by user itself – Huwaiza Feb 05 '22 at 13:44
  • 1
    Currently, V2 supports video URLs out of the box: https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/media#:~:text=https%3A//video.twimg.com/ext_tw_video/1527322141724532740/pu/vid/320x568/lnBaR2hCqE%2DR_90a.mp4 – stbychkov Feb 22 '23 at 23:41