Jupyter:XSRF cookie 与 POST 不匹配

问题描述 投票:0回答:4

我正在尝试使用 Jupyter Rest API,使用在本地 Anaconda 上运行的 python 程序将文件传输到 docker 容器内的本地 Jupyter。

在稍微了解如何输入令牌之后,我已经成功执行了

requests.get()

现在我想执行

requests.post()
命令来传输文件。

配置:

  1. 在 Windows 的 docker 工具箱上运行的本地 docker 容器
  • docker 版本 17.04.0-ce,内部版本 4845c56
  • 张量流/张量流包括。 Jupyter最新版本安装
  • jupyter_kernel_gateway==0.3.1
  1. 在 Windows 10 计算机上运行的本地 Anaconda v. 4.3.14

代码:

token = token_code_provided_by_jupyter_at_startup
api_url = "http://192.168.99.100:8888/api/contents"
# getting the file's data from disk and converting into a json file
cwd = os.getcwd()
file_location = cwd+r'\Resources\Test\test_post.py'
payload = open(file_location, 'r').read()
b64payload = base64.encodestring(payload)
body = json.dumps({
            'content':b64payload,
            'name': 'test_post.py',
            'path': '/api/contents/',
            'format': 'base64',
            'type':'file'
        })
# getting the xsrf cookie
client = requests.session()
client.get('http://192.168.99.100:8888/')
csrftoken = client.cookies['_xsrf']
headers ={'Content-type': 'application/json', 'X-CSRFToken':csrftoken, 'Referer':'http://192.168.99.100:8888/api/contents', 'token':token}
response = requests.post(api_url, data=body, headers=headers, verify=True)

返回错误

[W 12:22:36.710 NotebookApp] 403 POST /api/contents (192.168.99.1): XSRF cookie does not match POST argument
[W 12:22:36.713 NotebookApp] 403 POST /api/contents (192.168.99.1) 4.17ms referer=`http://192.168.99.100:8888/api/contents
docker csrf jupyter-notebook jupyter
4个回答
6
投票

我的解决方案受到@SaintNazaire 的启发。在我的 Chrome 浏览器中,我打开 cookie 文件夹,发现 Cookies 中有重复的

_xsrf
项。我将它们全部删除并刷新了 Jupyter,然后一切顺利。


0
投票

实际上,使用 header token 进行身份验证时,不需要 xsrf cookie。

headers = {'Authorization': 'token ' + token}

参考 Jupyter Notebook 文档。

http://jupyter-notebook.readthedocs.io/en/latest/security.html


0
投票

我的 JupyterHub 部署位于组织代理后面。将以下内容添加到我的

singleuser.extraEnv
文件中的
values.yaml
是对我有用的唯一修复:

extraEnv:
    NO_PROXY: "<hub_url>,<hub-ip>,localhost,127.0.0.1"
    no_proxy: "<hub_url>,<hub-ip>,localhost,127.0.0.1"

参考:https://jupyterhub.readthedocs.io/en/latest/faq/troubleshooting.html#proxy-settings-403-get


-1
投票

只需使用 jupyter 笔记本转到 Chrome 或默认浏览器设置即可。更改 cookie 设置以允许第三方 cookie。我想这会解决你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.