【问题标题】:Passing CSRF token传递 CSRF 令牌
【发布时间】:2014-10-12 22:25:50
【问题描述】:

这不会通过登录屏幕。我认为我没有正确传递 CSRF 令牌。我该怎么做?

from bs4 import BeautifulSoup
import requests

url = 'https://app.greenhouse.io/people/new?hiring_plan_id=24047'
cookies = {'_session_id':'my_session_id'}
client = requests.session()

soup = BeautifulSoup(client.get(url, cookies=cookies).content)
csrf_metatags = soup.find_all('meta',attrs={'name':'csrf-token'})[0].get('content')
posting_data = dict(person_first_name='Morgan') ## this is what I want to post to the form
headers = dict(Referer=url, csrf_token=csrf_metatags)
r = client.post(url, data=posting_data, headers=headers)

谢谢!

【问题讨论】:

  • 尝试将 csrf 令牌放入 posting_data 字典中

标签: python post csrf


【解决方案1】:

如果您检查代码,您会发现表单有一个隐藏的附加值,如下所示:

<input name="authenticity_token" type="hidden"
value="2auOlN425EcdnmmoXmd5HFCt4PkEOhq0gpjOCzxNKns=" />

您可以通过以下方式捕获此值:

csrf_data = soup.find("input", {"name": "authenticity_token"}).get("value")

现在将值重新附加到发布数据,就像您对 person_first_name 所做的那样:

posting_data = dict(person_first_name='Morgan',
                    authenticity_token=csrf_data)

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 2014-10-12
    • 2018-10-23
    • 2019-06-01
    • 2018-02-02
    • 2015-01-31
    • 2017-04-12
    • 2015-05-13
    相关资源
    最近更新 更多