Login into codeforces using python (Beautiful Soup and Requests)

Viewed 204

I recently tried to login into codeforces using python but failed in doing so I have been successful in fetching the csrf token but whenever I send a request after that it returns Error 403 : Request Forbidden, I would be really thankful for someone who would help :

import pyautogui
import clipboard
import requests
from bs4 import BeautifulSoup
import re

#user_name = input()
#psd = input()

base = "http://codeforces.com"
service_url = "{base}/{login}".format(base=base, login="enter")
dt = requests.get("http://codeforces.com/enter?back=%2F")
dt = dt.text

ss=BeautifulSoup(dt, 'html.parser')
print(ss)

token = ss.find(id='body').find(style = 'position: relative;').findNext(style = 'position: relative;')
token = token.find(id = 'pageContent')
print(token)
token = token.find(**{'class': 'enterPage'})
token = token.find(**{'class': 'roundbox'})
token = token.find('form')
print(token)

csrf_token = token.find(action = "").get('value')
print(csrf_token)
token = token.find(**{'class': 'table-form'})
token = token.find('input')
print(token)
ftaa = token.findNext('input').get('value')
print(ftaa)
bfaa = token.findNext('input').get('value')
print(bfaa)

print(csrf_token, ftaa, bfaa)
usr_name = input()
psd = input()

payload = {
  'csrf_token': csrf_token,
  'action': 'enter',
  #'ftaa': ftaa,
  #'bfaa': bfaa,
  'handle': usr_name,
  'password': psd
           }

data = requests.post(service_url, data=payload)
data = data.text
soup = BeautifulSoup(data, 'html.parser')
soup = soup.prettify()
print(soup)


1 Answers

I think you need to add the csrf token in the headers as well

import requests
from bs4 import BeautifulSoup

base = "https://codeforces.com"
service_url = "{base}/{login}".format(base=base, login="enter")

s = requests.session()
dt = s.get(service_url)
dt = dt.text
ss = BeautifulSoup(dt, 'html.parser')
with open("login.html", "w") as file:
    file.write(str(ss))
csrf_token = ss.find_all("span", {"class": "csrf-token"})[0]["data-csrf"]
print(csrf_token)

usr_name = input("User name: ")
psd = input("Password: ")

headers = {
    'X-Csrf-Token': csrf_token
}
payload = {
'csrf_token': csrf_token,
'action': 'enter',
'handleOrEmail': usr_name,
'password': psd,
}
data = s.post(service_url, data=payload, headers=headers)
data = data.text
soup = BeautifulSoup(data, 'html.parser')
print(soup)
with open("output.html", "w") as file:
    file.write(str(soup))

logout = soup.select_one("a[href*=logout]")["href"]
data = s.get(base + logout)
soup = BeautifulSoup(data.text, "html.parser")
with open("logout.html", "w") as file:
    file.write(str(soup))

The website adds it with this code:

$.ajaxSetup({ scriptCharset: "utf-8" ,contentType: "application/x-www-form-urlencoded; charset=UTF-8", headers: {
'X-Csrf-Token': Codeforces.getCsrfToken()
}});
Related