logging website python brings up error -- Validation of the provided anti-forgery token failed

557 Views Asked by At

I am using Python to login to a website https://app.whiskeysystems.com/ and download certain files from it. However, I am getting the error

The cookie __RequestVerificationToken and the form field __RequestVerificationToken were swapped

I have tried providing the Antiforgery token, but still unable to understand what really is the problem.

Following is my current code (Python 2.7 on Windows 7, will also be using this code on MAC OS X):

import bs4

from bs4 import BeautifulSoup 

import requests 

import re 

import sys

import time

USER_AGENT = []


USER_AGENT.append('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36')

USER_AGENT.append('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36')

USER_AGENT.append('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36')

USER_AGENT.append('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1')

basic_URL = "https://app.whiskeysystems.com/Account/Login/"

complete_URL = basic_URL

print("\n**** Starting Program **** ")

print ("\nThe user-agent selected ---> " + USER_AGENT[2])
print("\nChecking if website ---> " + basic_URL +"  is UP or DOWN ?")

with requests.Session() as client:

    client.headers.update({'User-Agent':USER_AGENT[2]})
    c2 = client.get(complete_URL)  

    print("\nHTTP Status Code ---> " + str(c2.status_code))


    if ( int(c2.status_code) == 200):
        print("\nSuccessful!!!. Website is UP")

        print ("\nWill try to login to website using provided credentials")

        time.sleep(2)

        csrftoken = client.cookies['__RequestVerificationToken']
        print("\nThe __RequestVerificationToken value ---> " + str(csrftoken))

        time.sleep(1)
        login_data = dict(__RequestVerificationToken=csrftoken,Email='[email protected]', Password='123456') 
        r1 = client.post(complete_URL, data=login_data)
        print r1.text


    else:
        print("\n Error !!!. Quitting the program, please see the website is UP or not. ")
0

There are 0 best solutions below