Skip to content

Commit

Permalink
[ie/twitter] Fix guest token extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
bashonly committed Jul 17, 2023
1 parent 613dbce commit b13f71b
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions yt_dlp/extractor/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,8 @@ def is_logged_in(self):
return bool(self._get_cookies(self._API_BASE).get('auth_token'))

def _fetch_guest_token(self, display_id):
guest_token = traverse_obj(self._download_json(
f'{self._API_BASE}guest/activate.json', display_id, 'Downloading guest token', data=b'',
headers=self._set_base_headers(legacy=display_id and self._configuration_arg('legacy_api'))),
('guest_token', {str}))
if not guest_token:
raise ExtractorError('Could not retrieve guest token')
return guest_token
webpage = self._download_webpage('https://twitter.com/', display_id, 'Downloading guest token')
return self._search_regex(r'\.cookie\s*=\s*["\']gt=(\d+);', webpage, 'guest token')

def _set_base_headers(self, legacy=False):
bearer_token = self._LEGACY_AUTH if legacy and not self.is_logged_in else self._AUTH
Expand Down Expand Up @@ -185,13 +180,10 @@ def _perform_login(self, username, password):
if self.is_logged_in:
return

webpage = self._download_webpage('https://twitter.com/', None, 'Downloading login page')
guest_token = self._search_regex(
r'\.cookie\s*=\s*["\']gt=(\d+);', webpage, 'gt', default=None) or self._fetch_guest_token(None)
headers = {
**self._set_base_headers(),
'content-type': 'application/json',
'x-guest-token': guest_token,
'x-guest-token': self._fetch_guest_token(None),
'x-twitter-client-language': 'en',
'x-twitter-active-user': 'yes',
'Referer': 'https://twitter.com/',
Expand Down

0 comments on commit b13f71b

Please sign in to comment.