繁体   English   中英

包含json.dumps时python请求授权失败

[英]python requests authorization fails when json.dumps included

编辑:

如何在python中复制以下curl请求?

curl https://api.box.com/2.0/files/content
-H "Authorization: Bearer TOKEN" -d '{"name": "Wolves owners.ppt", "parent": {"id": "2841136315"}, "size": 15243}' 
-X OPTIONS`

当我向请求引入json.dumps时授权失败

以下请求正文的格式不正确。 这只是一个python字典,嵌套属性没有通过。

h.kwargs['body'] = {"size": 45810, "parent": {"id": "2841136315"}, "name": "83db7037-2037-4f38-8349-255299343e6d"}

first = requests.options(
        h.kwargs['url'], headers=h.headers, data=h.kwargs['body']
    )

查看响应,将主体设置为size=45810&parent=id&name=83db7037-2037-4f38-8349-255299343e6d

标头是

`{'Content-Length': '62', 'Content-Type': 'application/x-www-form-urlencoded', 'Accept-Encoding': 'gzip, deflate', 'Authorization': 'Bearer TOKEN', 'Accept': '*/*', 'User-Agent': 'python...'}`

在第二个请求上,一旦引入json.dumps,授权将失败并显示401

second = requests.options(
        h.kwargs['url'], headers=h.headers, data=json.dumps(h.kwargs['body'])
    )

标头是

`{'Content-Length': '95', 'Accept': '*/*', 'User-Agent': 'python...', 'Accept-Encoding': 'gzip, deflate', 'Authorization': 'Bearer TOKEN'}`

如何格式化正文以通过呼叫而不弄乱授权?

我尝试将第二个请求的内容类型设置为json,但是没有用

编辑2

我发现id字段是必填字段。 您的请求没有id值(父文件夹的id),这就是为什么您收到HTTP状态400的原因。这里的API似乎有点奇怪; 为什么父文件夹ID需要指定两次?

无论如何,最小的必填字段是parentid namesize不是必需的,但是将检查它们的值(如果提供),即名称已验证并且大小与配额和可用存储空间进行了比较。

我还发现,省略id字段时,curl和Python会生成相同的400响应。 也许您在卷曲测试中包含了id

最后, Content-type标头不起作用。

这是修改后的Python代码。

import requests
import json

url = 'https://api.box.com/2.0/files/content'
h.headers = {'Authorization':'Bearer TOKEN'}
parent_folder_id = "2843500681"    # replace with your folder id

h.kwargs = {"name": "Wolves owners.ppt",
            "parent": {"id": parent_folder_id},
            "id": parent_folder_id,
            "size": 15243}

resp = requests.options(url, headers=h.headers, data=json.dumps(h.kwargs))

编辑1:问题更新后

发送与curl相同的请求:

import requests

url = 'https://api.box.com/2.0/files/content'
h.headers = {'Content-type': 'application/x-www-form-urlencoded',
             'Authorization':'Bearer TOKEN'}    

h.kwargs = {"name": "Wolves owners.ppt",
            "parent": {"id": "2841136315"},
            "size": 15243}

resp = requests.options(url, headers=h.headers, data=json.dumps(h.kwargs))

请注意,这与curl一样,将Content-type标头显式设置为application/x-www-form-urlencoded 否则,请求中不会有明显差异。

还要注意,此请求中的主体实际上不是application/x-www-form-urlencoded ,而只是一个字符串。 将Content-type设置为application/json似乎更合适。

这是curl请求:

OPTIONS /2.0/files/content HTTP/1.1
User-Agent: curl/7.32.0
Host: api.box.com
Accept: */*
Authorization: Bearer TOKEN
Content-Length: 76
Content-Type: application/x-www-form-urlencoded

{"name": "Wolves owners.ppt", "parent": {"id": "2841136315"}, "size": 15243}

这是上面的代码生成的请求:

OPTIONS /2.0/files/content HTTP/1.1
Host: api.box.com
Content-Length: 76
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.5.0 CPython/2.7.5 Linux/3.17.4-200.fc20.x86_64
Connection: keep-alive
Content-type: application/x-www-form-urlencoded
Authorization: Bearer TOKEN

{"name": "Wolves owners.ppt", "parent": {"id": "2841136315"}, "size": 15243}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM