[PR #1] Fix Twitter/X authentication errors via session persistence and endpoint updates #1

Open
opened 2026-02-28 01:22:18 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/USCMig/twikit/pull/1
Author: @Copilot
Created: 1/31/2026
Status: 🔄 Open

Base: mainHead: copilot/add-export-bookmarks-script


📝 Commits (9)

  • 73cbe72 Initial plan
  • e314e4a Add export_bookmarks.py script with documentation
  • 6a7465e Add defensive error handling to export_bookmarks.py
  • cde5801 Update .gitignore to exclude pycache
  • 3f4c215 Add test script for export_bookmarks.py validation
  • 61e8c3a Add clarifying comments based on code review feedback
  • 3a07c21 Fix 403 authentication error with cookie-based session management
  • d407835 Add comprehensive troubleshooting guide for authentication issues
  • 14efcca Fix 404 error by updating onboarding API endpoints to internal API paths

📊 Changes

7 files changed (+917 additions, -3 deletions)

View changed files

.env.example (+7 -0)
📝 .gitignore (+5 -1)
scripts/README.md (+124 -0)
scripts/TROUBLESHOOTING.md (+291 -0)
scripts/export_bookmarks.py (+326 -0)
scripts/test_export_bookmarks.py (+162 -0)
📝 twikit/client/v11.py (+2 -2)

📄 Description

Twitter/X login was failing with 403 (rate limiting from repeated logins) and 404 (deprecated endpoint paths).

Changes

Session persistence prevents 403 rate limits

  • export_bookmarks.py: Add cookies_file parameter to client.login()
  • Session auto-persists to OUTPUT_DIRECTORY/twitter_session.json
  • Subsequent runs skip login entirely, reusing saved session
  • Eliminates repeated authentication attempts that trigger 403s

API endpoint migration fixes 404s

  • twikit/client/v11.py: Update onboarding endpoints from public API to internal API pattern
    • ONBOARDING_TASK: https://api.x.com/1.1/onboarding/task.jsonhttps://x.com/i/api/1.1/onboarding/task.json
    • ONBOARDING_SSO_INIT: Similar migration to /i/api/ path
  • Aligns with Twitter/X's current endpoint structure where auth flows use x.com/i/api while public data uses api.x.com

Error handling and documentation

  • export_bookmarks.py: Add targeted error messages for 403, 404, 429, 401 with actionable solutions
  • scripts/TROUBLESHOOTING.md: New 7KB guide covering all auth error scenarios, best practices, session management
  • scripts/README.md: Add troubleshooting section with session persistence explanation
# Before: fresh login every run → 403 errors
await client.login(auth_info_1=username, auth_info_2=email, ******

# After: session persistence
cookies_file = output_path / 'twitter_session.json'
await client.login(
    auth_info_1=username,
    auth_info_2=email,
    ******
    cookies_file=str(cookies_file)
)

First run: authenticates and saves session
Subsequent runs: loads session, no authentication, 2-3s faster, no rate limit risk

Original prompt

Create a standalone Python script at scripts/export_bookmarks.py that uses the twikit Python library (run via UV) to export bookmarked tweets into a single Markdown file for Obsidian. Requirements:

  • Use a .env file to load credentials with variables TWITTER_USERNAME, TWITTER_PASSWORD, TWITTER_EMAIL and an OUTPUT_DIRECTORY for the destination path.
  • Script accepts a date argument in YYYY-MM-DD format to limit how far back to pull bookmarked tweets from that date.
  • Each run outputs a single Markdown file named bookmarks_YYYY_MM_DD.md in OUTPUT_DIRECTORY.
  • The Markdown should include: author/handle, tweet content, embedded image links (Markdown ![](...)), links in the post, and tags/links suitable for Obsidian linking.
  • Assume UV is already installed; do not add setup for UV beyond what is needed for running the script.

Implement any necessary helpers and minimal documentation/comments to clarify usage.

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/USCMig/twikit/pull/1 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 1/31/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `copilot/add-export-bookmarks-script` --- ### 📝 Commits (9) - [`73cbe72`](https://github.com/USCMig/twikit/commit/73cbe725919b8b601777fa5439d7f9283d990af5) Initial plan - [`e314e4a`](https://github.com/USCMig/twikit/commit/e314e4acfa1cce395ce293bb2231450ae752698c) Add export_bookmarks.py script with documentation - [`6a7465e`](https://github.com/USCMig/twikit/commit/6a7465ea36d60fcaf0bc107d00e8a1cdc2e3fdb3) Add defensive error handling to export_bookmarks.py - [`cde5801`](https://github.com/USCMig/twikit/commit/cde58014df7b613e35c3b1d224c016d8b240aee6) Update .gitignore to exclude __pycache__ - [`3f4c215`](https://github.com/USCMig/twikit/commit/3f4c21507b879ecb05eef3998a3202d5ecd68cdf) Add test script for export_bookmarks.py validation - [`61e8c3a`](https://github.com/USCMig/twikit/commit/61e8c3a5bcd69294dbe49b12336861414d7785ed) Add clarifying comments based on code review feedback - [`3a07c21`](https://github.com/USCMig/twikit/commit/3a07c211ba77d2275d4edcc34c5d96d922c7ab85) Fix 403 authentication error with cookie-based session management - [`d407835`](https://github.com/USCMig/twikit/commit/d407835592a6031080ea33e69c731e287a8e68c2) Add comprehensive troubleshooting guide for authentication issues - [`14efcca`](https://github.com/USCMig/twikit/commit/14efccafd92778fafc31a4339a73caba8bdbafff) Fix 404 error by updating onboarding API endpoints to internal API paths ### 📊 Changes **7 files changed** (+917 additions, -3 deletions) <details> <summary>View changed files</summary> ➕ `.env.example` (+7 -0) 📝 `.gitignore` (+5 -1) ➕ `scripts/README.md` (+124 -0) ➕ `scripts/TROUBLESHOOTING.md` (+291 -0) ➕ `scripts/export_bookmarks.py` (+326 -0) ➕ `scripts/test_export_bookmarks.py` (+162 -0) 📝 `twikit/client/v11.py` (+2 -2) </details> ### 📄 Description Twitter/X login was failing with 403 (rate limiting from repeated logins) and 404 (deprecated endpoint paths). ## Changes ### Session persistence prevents 403 rate limits - `export_bookmarks.py`: Add `cookies_file` parameter to `client.login()` - Session auto-persists to `OUTPUT_DIRECTORY/twitter_session.json` - Subsequent runs skip login entirely, reusing saved session - Eliminates repeated authentication attempts that trigger 403s ### API endpoint migration fixes 404s - `twikit/client/v11.py`: Update onboarding endpoints from public API to internal API pattern - `ONBOARDING_TASK`: `https://api.x.com/1.1/onboarding/task.json` → `https://x.com/i/api/1.1/onboarding/task.json` - `ONBOARDING_SSO_INIT`: Similar migration to `/i/api/` path - Aligns with Twitter/X's current endpoint structure where auth flows use `x.com/i/api` while public data uses `api.x.com` ### Error handling and documentation - `export_bookmarks.py`: Add targeted error messages for 403, 404, 429, 401 with actionable solutions - `scripts/TROUBLESHOOTING.md`: New 7KB guide covering all auth error scenarios, best practices, session management - `scripts/README.md`: Add troubleshooting section with session persistence explanation ```python # Before: fresh login every run → 403 errors await client.login(auth_info_1=username, auth_info_2=email, ****** # After: session persistence cookies_file = output_path / 'twitter_session.json' await client.login( auth_info_1=username, auth_info_2=email, ****** cookies_file=str(cookies_file) ) ``` **First run**: authenticates and saves session **Subsequent runs**: loads session, no authentication, 2-3s faster, no rate limit risk <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > Create a standalone Python script at `scripts/export_bookmarks.py` that uses the twikit Python library (run via UV) to export bookmarked tweets into a single Markdown file for Obsidian. Requirements: > > - Use a `.env` file to load credentials with variables `TWITTER_USERNAME`, `TWITTER_PASSWORD`, `TWITTER_EMAIL` and an `OUTPUT_DIRECTORY` for the destination path. > - Script accepts a date argument in `YYYY-MM-DD` format to limit how far back to pull bookmarked tweets from that date. > - Each run outputs a single Markdown file named `bookmarks_YYYY_MM_DD.md` in `OUTPUT_DIRECTORY`. > - The Markdown should include: author/handle, tweet content, embedded image links (Markdown `![](...)`), links in the post, and tags/links suitable for Obsidian linking. > - Assume UV is already installed; do not add setup for UV beyond what is needed for running the script. > > Implement any necessary helpers and minimal documentation/comments to clarify usage. </details> <!-- START COPILOT CODING AGENT SUFFIX --> *This pull request was created from Copilot chat.* > <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/twikit-USCMig#1
No description provided.