gpt4 book ai didi

php - 如何从 Laravel 获取 CSRF token 并将其传递到 python 脚本中

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:31 30 4
gpt4 key购买 nike

我正在使用oauth2-server-laravel对将数据导出到 csv 文件的 api 进行身份验证。文档中有这个要求。

In order to make some the authorization and resource server work correctly with Laravel5, remove the App\Http\Middleware\VerifyCsrfToken line from the $middleware array and place it in the $routeMiddleware array like this: 'csrf' => App\Http\Middleware\VerifyCsrfToken::class,

Note: remember to add the csrf middleware manually on any route where it's appropriate.

问题是我的应用程序很大,我宁愿不手动更改所有其他路由。所以我遇到了X-CSRF-TOKEN在文档中。

我的问题是:如何获取 csrf token ,以便将其附加到 Python 中的 header 参数中?文档说我可以使用 HTML 标签 meta 但我没有使用任何 HTML 文件来执行此任务。

此外,我无法使用 session ,因为我不需要登录即可访问 API。

最佳答案

更好的解决方案:您可以像BeautifulSoup一样通过网络scraper轻松获得 token

import requests
from bs4 import BeautifulSoup

r = requests.get(url) #url is create form
soup = BeautifulSoup(r.text, 'html.parser')
for link in soup.find_all('input'):
token = link['value']

当然,请注意 Laravel 还会为每个表单生成一个 cookie。因此,您需要将 cookie 与您的发布请求一起发送。

jar = requests.cookies.RequestsCookieJar()
jar.set('XSRF-TOKEN', r.cookies['XSRF-TOKEN'])

现在您可以轻松发送您的帖子请求。例如,我正在发送登录请求

url_post = "http://localhost/login"    
data = {
"_token": token,
"email": "example@email.com",
"password": "password"
}


login_request = requests.post(url_post, data=data, cookies=jar)
print(r.text)

关于php - 如何从 Laravel 获取 CSRF token 并将其传递到 python 脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39848892/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com