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
You can get a fresh
x-guest-token
viai. 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.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: