POST 响应 headers 中不存在位置字段

Location field not present in POST response headers

我正在尝试使用 Python 请求库登录网页。我正在向 link 发送 POST 请求并收到回复,但我在回复 headers 中缺少 'Location' 项。我正在使用以下代码:

import requests
from bs4 import BeautifulSoup

url = 'https://aab.powerapp.nl/login'

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content)
token = soup.find('input', {'name': '_token'}).get('value')
headers['Cookie'] = '; '.join([x.name + '=' + x.value for x in response.cookies])
headers['Content-Type'] = 'application/x-www-form-urlencoded'
payload = {
        '_token': token,
        'emailaddress': '', 
        'password': ''
}
response = requests.post(url + '/validate', files=payload, headers=headers)

如Chrome所示的请求headers如下:

POST /login/validate HTTP/1.1
Host: aab.powerapp.nl
Connection: keep-alive
Content-Length: 90
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Origin: https://aab.powerapp.nl
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: https://aab.powerapp.nl/login
Accept-Encoding: gzip, deflate, br
Accept-Language: nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7
Cookie: XSRF-TOKEN=eyJpdiI6InkyK1pWbzRcL3dmbVRPeVwvNW52UjFyUT09IiwidmFsdWUiOiJWazZXRGFDb0tWcGQ1WjhleHdSRzZYU202WXN3bG4wbG9YQnZZcUNYTndDcXJETDl2MGIrY0dnUVlOcUhoTmZKIiwibWFjIjoiZmNmNzlmMjc5YWRiMmMyNjk0MmI1YjE5ZTE2OTNmZGU5YTRlNDA0MDg3ZGE4NTI1ZTc3NTBkZjg0MjFjOTQzNSJ9; pa_session_aabp=eyJpdiI6IjlidWIxdlVybFl0UjJHOXorcnFmT2c9PSIsInZhbHVlIjoiM0JZXC9NNHJOWlFIVjBhQ2xxelN2ZENrSVBuTkpibzJybUhkSHRUeGpSTTRITnFyK2E1M3ExM2VjWjNnNG4rbXMiLCJtYWMiOiJmN2NjZGRkNjc2YTJkZjBjNGVkZDczODNlZDI1YTk5YTllODk1YmFiMDc4Mzc5MWM2ZDZmMThiZjNmMzU0MmU5In0%3D

我只收到以下 headers:

{'Date': 'Sat, 29 Jun 2019 15:40:15 GMT', 'Server': 'Apache', 'Cache-Control': 'no-cache, private', 'Set-Cookie': 'pa_session_aabp=eyJpdiI6ImFwS21QNVpMRGh1ZHJ0ZW1vbCtia1E9PSIsInZhbHVlIjoiRHZHa1wvZlNKMnkzZVEzTCthQVZ1eGdjSmRVbEZVSVJ2RHN5ZXZwNEV6NmRkZVZnTVhTMVlGUWxES1dYUmdvQlgiLCJtYWMiOiIyNWRhYTRkNzUyZTdiYmI4ODYwMDBkMmVjMDRmNzliYzZmZDAzNzg4N2NiMTdkMWI3Y2YyZjQyOGI0MDFiZWJlIn0%3D; path=/; domain=aab.powerapp.nl; secure; httponly', 'Location': 'https://aab.powerapp.nl/login', 'Keep-Alive': 'timeout=5, max=100', 'Connection': 'Keep-Alive', 'Transfer-Encoding': 'chunked', 'Content-Type': 'text/html; charset=UTF-8', 'Content-Language': 'nl'}

我已尝试将 allow_redirects 参数设置为 TrueFalse,但我仍然无法在响应中返回 Location 项目 headers.除此之外,我尝试设置几个不同的请求 headers,包括 CookieRefererUser-Agent。除了在请求中作为表单数据发送的 emailaddresspassword 之外,还发送了第三个字段 _token。我不确定这是否是问题的原因,因为我也不确定令牌的价值是如何决定的。

编辑: 我发现可以从登录页面的 html 中提取也作为表单数据发送的令牌。我对我的脚本做了一些修改以反映这些知识。不幸的是,这似乎并没有解决我的问题。

您应该在开始时每个 session 发送一次您的凭据,并且在您成功通过该过程后,您将获得在服务器端定义的一段时间内有效的令牌字符串。授权后对 api 的任何其他调用都需要将该令牌作为 header.

发送

我已经设法通过使用请求 session 结合使用数据参数而不是文件参数来修复它。代码如下:

import requests
from bs4 import BeautifulSoup

session = requests.session()
url = 'https://aab.powerapp.nl/login'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
           "Upgrade-Insecure-Requests": "1"}
response = session.get(url, headers=headers)
soup = BeautifulSoup(response.content)
token = soup.find('input', {'name': '_token'}).get('value')
headers['Content-Type'] = 'application/x-www-form-urlencoded'
payload = {
        '_token': token,
        'emailaddress': '', 
        'password': ''
}
response = session.post(url + '/validate', data=payload, headers=headers, allow_redirects=False)