【问题标题】:Jupyter: XSRF cookie does not match POSTJupyter:XSRF cookie 与 POST 不匹配
【发布时间】:2017-05-20 16:59:35
【问题描述】:

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

我已经成功地执行了一个 requests.get() 在对如何输入令牌进行了一些处理之后。

现在我想执行 requests.post() 命令来传输文件。

配置:

  1. 在 windows 的 docker 工具箱上运行的本地 docker 容器

    • docker 版本 17.04.0-ce,构建 4845c56
    • 张量流/张量流包括。 Jupyter 最新版本安装
    • jupyter_kernel_gateway==0.3.1
  2. 在 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 与 POST 参数不匹配 [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


    【解决方案1】:

    其实在使用header token进行认证时,不需要xsrf cookie。

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

    参考了 Jupyter 笔记本文档。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 2017-09-13
      • 2019-11-25
      相关资源
      最近更新 更多