curl to python请求转换

时间:2017-05-31 23:58:00

标签: python curl python-requests dropbox

我正在使用Dropbox的python API,尝试将以下curl命令转换为Python请求:

curl -X POST https://content.dropboxapi.com/2/files/download \
        --header "Authorization: Bearer <ACCESS_TOKEN>" \
        --header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Prime_Numbers.txt\"}"

到目前为止我的翻译:

downloadHeader={"Authorization: " + authorization}

downloadURL = "https://content.dropboxapi.com/2/files/download"

downloadPayload = {"Dropbox-API-Arg": {"path": "/" + dbPATH}}

downloadResponse = requests.post(downloadURL, data=json.dumps(downloadPayload), headers=downloadHeader)

然而,当我运行此操作时,我收到以下错误:

  

表示headers.items()中的标题:       AttributeError:&#39; set&#39;对象没有属性&#39;项目&#39;

有人能给我一些反馈吗?我对我的授权价值充满信心,因为它在一个单独的请求中工作,我在下面复制和粘贴:

MDlink = "https://api.dropboxapi.com/2/sharing/get_shared_link_metadata"
authorization = "Bearer " + ACCESS_TOKEN
headers={"Content-Type":"application/json", "Authorization": authorization}
payload = {"url": imageLink}
response = requests.request("POST", MDlink, data=json.dumps(payload), headers=headers)

非常感谢!

1 个答案:

答案 0 :(得分:1)

你想要这个

downloadHeader={"Authorization: " + authorization}

downloadHeader={"Authorization": authorization}

或更准确地说

downloadHeader={"Authorization": "Bearer <ACCESS_TOKEN>"}

说明:

{1} # this is a set. It has no .items()
{1: 1} # this is a dict. You can call .items()