skip to Main Content

I want to download twitter video without using twitter’s official API . So when i open an icognito and chrome dev tool in chrome and request any video like “https://twitter.com/KTHopkins/status/1248140219490209792
i see twitter requests two important headers to server the video
1.‘authorization’
2. ‘x-guest-token’

which i seem to cannot get where these tokens are generated from ? Belown is my python request which i am sending .

import requests

headers = {

    'authority': 'api.twitter.com',

    'dnt': '1',

    'x-twitter-client-language': 'en',

    # 'x-csrf-token': '6089ceeab3324243e7b952679b2b7851',

    'authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAANk3DgEAAAAAB0pZx4xjgXBOoalj%2FRbagurxD2M%3DG8634UVlBud8LrLG4nGo7FpN2RCO2xul5BuPKHuejUAV14O0KG',

    'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',

    'sec-fetch-dest': 'empty',

    'x-guest-token': '1248286947669237760',

    'x-twitter-active-user': 'yes',

    'accept': '*/*',

    'origin': 'https://twitter.com',

    'sec-fetch-site': 'same-site',

    'sec-fetch-mode': 'cors',

    'accept-language': 'en-US,en;q=0.9,hi;q=0.8',

    # 'cookie': 'personalization_id="v1_tWyK8Fn5ofSPjSAEKsnyrw=="; guest_id=v1%3A158644503604220835; ct0=6089ceeab3324243e7b952679b2b7851; _twitter_sess=BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCDI2fl9xAToMY3NyZl9p%250AZCIlNmM2YjZiYTU4MzdhY2FkNDQwZjcwMGU1NDliNzEzN2Y6B2lkIiViOWUx%250AYzM5MDk3ZTQ0YzMyZDRkMGU3YTdkM2FlMGY2YQ%253D%253D--223c07ac4708a9bec30dec1e0e9c3d52544b310c; _ga=GA1.2.162154316.1586445033; _gid=GA1.2.1445748635.1586445033; gt=1248286947669237760',

}

response = requests.get('https://api.twitter.com/2/timeline/conversation/1248293309950255107.json')

print(response.text)

so help me in geeting those two tokens .

2

Answers


  1. You can get a fresh x-guest-token via

    curl -skL https://twitter.com/ -H 'User-Agent: Firefox' --compressed | grep -o 'gt=[0-9]*' | sed s.gt=..
    

    i. e., by download the website’s content and parse its JavaScript. Note: The User-Agent needs to be set to something that Twitter believes to understand JavaScript. Otherwise, they send a legacy version of the website which doesn’t contain the token.

    Login or Signup to reply.
  2. This github repo contains a python script that will download twitter videos. You can look at the source to see exactly how to do it.

    The high level of what you need to do is:

    1. Get the bearer token (The html of the twitter link you go to links a file called main.some random numbers.js. Within that javascript file is the bearer token.
    2. Take the bearer token and call https://api.twitter.com/1.1/guest/activate.json using the bearer token as an authorization header

    curl ‘https://api.twitter.com/1.1/guest/activate.json’ -X POST -H ‘authorization: Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA’

    1. This should return json containing a valid guest token.
    2. With the bearer token and guest token you can then make requests as if you were an unauthenticated user. To download the video you’ll need to find the list of mp4 and m4s files that compose the video (in the resolution you want) and then download them and reconstitute them into an mp4 file. As I mentioned the linked github project has the source for all this. If you want the details look there!
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search