Crisp Apples
Crisp Apples

Reputation: 176

Can't get CSRF token all the time

I'm trying to get the csrf token on the Spotify login page. I used to have no problem doing it, but it seems I can only get the token 50% of the time now. I tried this simple code I made:

import requests


def Main():

    s = requests.session()
    s.get("https://accounts.spotify.com/en/login/?_locale=en-US&continue=https:%2F%2Fwww.spotify.com%2Fus%2Faccount%2Foverview%2F")
    print(s.cookies)
    print(s.cookies.get_dict())

    try:
        print(s.cookies['csrf_token'])
    except KeyError:
        Main()


Main()

If you run the program you'll see that the CSRF is only printed a handful times while nothing is printed all the other times. Why is requests only able to get the CRSF cookie/token only sometimes and not always? I've never had this problem before.

Edit: My requests version is 2.21.0

Upvotes: 1

Views: 1361

Answers (1)

KC.
KC.

Reputation: 3107

The reason you don't get csrf_token intermittently, because you frequently access website or you access it without headers --- server considers you might be bot or attacker. These reasons will lead to server responds 40x or 50x. So you should add headers and set interval to 1 second.

import requests
import time

def Main():
    time.sleep(1)
    with requests.session() as s:
        s.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate, br"
        }
        resp = s.get("https://accounts.spotify.com/en/login/?_locale=en-US&continue=https:%2F%2Fwww.spotify.com%2Fus%2Faccount%2Foverview%2F")
        print(s.cookies['csrf_token'])

[Main() for _ in range(10)]

Upvotes: 1

Related Questions