Skip to content

fix: resolve sign-in authentication failure#83

Open
milanbeherazyx wants to merge 1 commit intorongardF:mainfrom
milanbeherazyx:fix/signin-authentication
Open

fix: resolve sign-in authentication failure#83
milanbeherazyx wants to merge 1 commit intorongardF:mainfrom
milanbeherazyx:fix/signin-authentication

Conversation

@milanbeherazyx
Copy link

@milanbeherazyx milanbeherazyx commented Mar 8, 2026

Problem

The __auth method in main.py silently fails with error while signin, causing all authenticated users to fall back to unauthorized_user_token (no-login mode). This has been a widely reported issue affecting many users.

Root Cause

The existing implementation uses a bare requests.post() call without:

  • A User-Agent header — TradingView flags the request as bot traffic
  • A requests.Session() — No cookies are persisted, triggering rate-limit / anti-bot protection
  • Proper Origin and Content-Type headers

As a result, TradingView returns a rate_limit error or silently rejects the sign-in request.

Fix

Updated the __auth method to properly authenticate with TradingView:

  • Use requests.Session() to maintain cookies across the GET → POST flow
  • Set a browser-like User-Agent to avoid bot detection
  • Visit the homepage first (GET /) before posting to /accounts/signin/ to establish a proper session
  • Add Origin and Content-Type headers for CORS compliance
  • Add a single retry with 2s delay when rate-limited
  • Improved error logging to include the actual exception message for easier debugging
  • Removed duplicate import json statement

Before (Broken)

error while signin
you are using nologin method, data you access may be limited
Token: unauthorized_user_token

After (Fixed)

https://www.tradingview.com:443 "GET / HTTP/1.1" 200
https://www.tradingview.com:443 "POST /accounts/signin/ HTTP/1.1" 200
Token: eyJhbGciOiJSUzUxMiIs...  (valid JWT auth token)

Testing

Tested with valid TradingView credentials — login now succeeds and returns a proper JWT auth_token. Data fetching with authenticated session works as expected (verified with NIFTY 15-min candles from NSE).

Backward Compatibility

  • No API changes — TvDatafeed(username, password) works the same way
  • No-login mode (TvDatafeed()) is unaffected
  • No new dependencies added

…d User-Agent

The __auth method was using a bare requests.post() without a session or User-Agent header, causing TradingView to reject the request as bot traffic (returning rate_limit or silently failing).

Changes:

- Use requests.Session() to maintain cookies across requests

- Add a browser-like User-Agent header to avoid bot detection

- Visit the homepage before POST to /accounts/signin/ to pick up session cookies

- Add Origin and Content-Type headers for proper CORS compliance

- Add a single retry with 2s delay when rate-limited

- Improve error logging to include the actual exception message

- Remove duplicate json import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants