无法检查CSRF令牌。请再试一次

2024-04-19 16:05:22 发布

您现在位置:Python中文网/ 问答频道 /正文

通过postman,其余的调用工作正常,所以我通过postman本身生成了python代码,并执行下面的代码。但我得到错误“无法检查CSRF令牌。请再试一次“ 有人能帮我搞清楚这是怎么回事吗?你知道吗

import requests, re

url = "https://openshiftnew.com/oauth/token/request"

headers = {
    'Content-Type': "application/x-www-form-urlencoded",
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
    'Authorization': "Basic ###########",
    'Accept-Encoding': "gzip, deflate"
    }

response = requests.request("GET", url, headers=headers, verify=False)
m = re.findall('value.*',response.text)
code = m[0].split('"')[1]
csrf = m[1].split('"')[1]

url = "https://openshiftnew.com/oauth/token/display"

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"code\"\r\n\r\n{code}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"csrf\"\r\n\r\n{csrf}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--".format(code=code, csrf=csrf)

headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'Content-Type': "application/x-www-form-urlencoded",
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.3",
    'Authorization': "Basic ########",
    'Cache-Control': "no-cache",
    'Host': "openshiftnew.com",
    'Accept-Encoding': "gzip, deflate",
    'Cookie': "csrf={csrf}".format(csrf=csrf),
    'Connection': "keep-alive",
    'cache-control': "no-cache"
    }
response = requests.request("POST", url, data=payload, headers=headers, verify=False)
print(response.text)

上面的代码尝试向OpenShift URL提交一个多部分表单数据请求


Tags: 代码textimageformurldataapplicationresponse