How to generate UserLoginType[_token] for login request

岁酱吖の 提交于 2021-02-08 08:49:11

问题


I'm trying to login into a website using a post request like this:

import requests

cookies = {
    '_SID': 'c1i73k2mg3sj0ugi5ql16c3sp7',
    'isCookieAllowed': 'true',
}

headers = {
    'Host': 'service.premiumsim.de',
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Referer': 'https://service.premiumsim.de/',
    'Content-Type': 'application/x-www-form-urlencoded',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1',
}

data = [
  ('_SID', 'c1i73k2mg3sj0ugi5ql16c3sp7'),
  ('UserLoginType[alias]', 'username'),
  ('UserLoginType[password]', 'password'),
  ('UserLoginType[logindata]', ''),
  ('UserLoginType[_token]', '1af70f3d0e5b9e6c39e1475b6d84e9d125d076de'),
]

requests.post('https://service.premiumsim.de/public/login_check', headers=headers, cookies=cookies, data=data)

The problem is 'UserLoginType[_token] in params. The above code is working, but I don't have that token and have no clue how to generate it. So when I do my request, I do it without the _token and the request fails. Google did not found any helpful information about UserLoginType.

Does anyone know how to generate it(e.g. with another request first) to be able to login?

Edit: Thanks to the suggestion of t.m.adam I used bs4 to get the token:

import requests
from bs4 import BeautifulSoup as bs

tokenRequest = requests.get('https://service.premiumsim.de')
html_bytes = tokenRequest.text
soup = bs(html_bytes, 'lxml')
token = soup.find('input', {'id':'UserLoginType__token'})['value']

来源:https://stackoverflow.com/questions/47103183/how-to-generate-userlogintype-token-for-login-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!