使用python请求维护身份验证

使用python请求维护身份验证,python,authentication,python-requests,Python,Authentication,Python Requests,尝试使用请求包保持登录网站,但失败: import requests # Test account payload = {"email": "ozeifaze@gmail.com", "password": "testpassword0?"} with requests.Session() as s: log = s.post("https://www.doctrine.fr/api/login

尝试使用请求包保持登录网站,但失败:

import requests

# Test account
payload = {"email": "ozeifaze@gmail.com", "password": "testpassword0?"}

with requests.Session() as s:
    log = s.post("https://www.doctrine.fr/api/login", json=payload)
    print(log) # <Response [200]> or <Response [422]> if wrong payload
    print(log.text) # {"ok":true,"mfa_required":false}

    page = s.get("https://www.doctrine.fr/dashboard")
    print(page) # <Response [200]> but not authentified and redirected
导入请求
#测试帐户
有效负载={“电子邮件”:ozeifaze@gmail.com,“密码”:“testpassword0?”}
将requests.Session()作为s:
log=s.post(“https://www.doctrine.fr/api/login,json=payload)
打印(日志)#或如果负载错误
打印(log.text)#{“ok”:true,“需要mfa”:false}
page=s.get(“https://www.doctrine.fr/dashboard")
打印(第页)#但未验证和重定向
POST请求起作用并返回所期望的内容。但是,当我尝试访问第二个页面时,我不再进行身份验证,并被重定向到另一个页面


我做错了什么?

这可能是由于第二次呼叫中没有发送cookies造成的

log = s.post("https://www.doctrine.fr/api/login", payload)
page = s.get("https://www.doctrine.fr/dashboard", ...)
比如说

>>> import requests
>>> login_data =  {'formPosted':'1', 'login_email':'me@example.com', 'password':'pw'}
>>> r = requests.post('https://localhost/login.py', login_data)
>>> 
>>> r.text
u'You are being redirected <a href="profilePage?_ck=1349394964">here</a>'
>>> r.cookies
{'session_id_myapp': '127-0-0-1-825ff22a-6ed1-453b-aebc-5d3cf2987065'}
>>> 
>>> r2 = requests.get('https://localhost/profile_data.json', ...)

这个问题的进展如何?我的回答有用吗?请更新状态。