fix: send session cookies when fetching x.com for ClientTransaction init - #86
Open
Blacksuite wants to merge 1 commit into
Open
fix: send session cookies when fetching x.com for ClientTransaction init#86Blacksuite wants to merge 1 commit into
Blacksuite wants to merge 1 commit into
Conversation
_ensure_client_transaction() fetched https://x.com without cookies, so x.com returned the logged-out landing page. That page contains no ondemand.s marker, so ON_DEMAND_FILE_REGEX.search() returns None and get_ondemand_file_url() raises "'NoneType' object has no attribute 'group'". ClientTransaction then stays None and every API request goes out without an x-client-transaction-id header, which X answers with HTTP 404 — including SearchTimeline and account verification endpoints. Measured against x.com: anonymous returns ~35KB with zero occurrences of "ondemand"; the same request with auth_token/ct0 cookies returns ~278KB with the marker present. _build_headers() already builds exactly this Cookie value for API calls; this reuses it for the init fetch. Fixes public-clis#78 Fixes public-clis#73 Fixes public-clis#69
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
_ensure_client_transaction()fetcheshttps://x.comto extract theondemand.sbundle used to buildx-client-transaction-id, but that request goes out without cookies. x.com serves the logged-out landing page to anonymous requests, and that page contains noondemand.smarker.So
ON_DEMAND_FILE_REGEX.search()returnsNone, andget_ondemand_file_url()raises:_client_transactionstaysNone, so every subsequent API call is sent without thex-client-transaction-idheader. X answers those with HTTP 404 — which is why the failure shows up as a 404 onSearchTimelineand on account-verification endpoints rather than as an auth error, and why retries never help.Evidence
Same request via
curl_cffiwithimpersonate="chrome", only difference being cookies:ondemandoccurrencesauth_token+ct0The anonymous response is a valid 200, which is why this fails silently at a warning level instead of surfacing as a request error.
Fix
Set the
Cookieheader on the init fetch._build_headers()already constructs exactly this value for API calls, so this reuses the sameself._cookie_string or auth_token=…; ct0=…form — no new configuration or code paths.Verified end-to-end after the change:
twitter searchreturns live results, andClientTransaction initialized for x-client-transaction-idis logged.Tests
Two regression tests added to
TestEnsureClientTransaction, covering the derived cookie pair and the full_cookie_stringpassthrough. Both fail onmainand pass with the fix:uv run ruff check .anduv run mypy twitter_cliboth clean.Fixes #78
Fixes #73
Fixes #69