Skip to content
This repository has been archived by the owner on Dec 26, 2021. It is now read-only.

Commit

Permalink
Fix: failing session renewals +
Browse files Browse the repository at this point in the history
  Sessions are failing to renew, because used request headers
  are set on the TwitterSession instance, but never reset.
  Thus, trying to login_guest() with an outdated X-Guest-Token
  will make the activate.json request for the new token be rejected
  because of that outdated token.

  Solution: Reset TwitterSession._headers in renew_session()
            to just have the User-Agent value before trying to login.
  • Loading branch information
rbeer committed Dec 7, 2019
1 parent b556b10 commit 1e4bc11
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ class TwitterSession:
twitter_auth_key = None

def __init__(self):
self._headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
}
# without token, shit be broken; or something
self._guest_token = None
self._csrf_token = None

Expand All @@ -78,6 +74,9 @@ def __init__(self):
# this stays `None` for guest sessions
self.username = None

# sets self._headers
self.reset_headers()

def set_csrf_header(self):
cookies = self._session.cookie_jar.filter_cookies('https://twitter.com/')
for key, cookie in cookies.items():
Expand All @@ -95,9 +94,15 @@ async def get_guest_token(self):
debug(str(self._headers))
return guest_token

def reset_headers(self):
self._headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
}

async def renew_session(self):
await self.try_close()
self._session = aiohttp.ClientSession()
self.reset_headers()

async def refresh_old_token(self):
if self.username is not None or self.next_refresh is None or time.time() < self.next_refresh:
Expand Down

0 comments on commit 1e4bc11

Please sign in to comment.