我正在开发一个将Verizon Thingspace REST API整合到Python程序中的项目。
一位同事为我提供了一个有效的CURL示例(我不熟悉Curl所以我试图转换为Python)。
curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" --header "VZ-M2M-Token: 621d9779-f8bc-4fe9-91dd-b726c52e7117" --header "Authorization: Bearer 89ba225e1438e95bd05c3cc288d3591" -d "{\"accountName\": \"TestAccount-1\"}" https://thingspace.verizon.com/api/m2m/v1/devices/actions/list
我正在尝试将这个完全相同的请求转换为Python函数。这就是我所拥有的:
import requests
def getList(token):
url = "https://thingspace.verizon.com/api/m2m/v1/devices/actions/list"
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer 89ba225e1438e95bd05c3cc288d3591",
"VZ-M2M-Token": "f7ef3a35-abb6-418b-92d4-7cdac8b06c5f",
}
data = {"accountName": "TestAccount-1"}
print data
deviceList = requests.post(url, data=data, headers=headers)
print headers
print (deviceList.status_code, deviceList.reason, deviceList.text)
return deviceList
当我运行它时,我在JSON中收到以下错误消息:
(400,'Bad Request', U'{ “错误码”: “REQUEST_FAILED.UnexpectedError”, “的errorMessage”:“莫非 不读文档:无法识别的令牌''accountName \':正在等待 (\'true \',\'false \'或\'null \')\ n在[来源: java.io.PushbackInputStream@51ceaf1d; line:1,column:13];嵌套 异常是com.fasterxml.jackson.core.JsonParseException: 无法识别的令牌''accountName \':期待(\'true \',\'false \' 或\'null \')\ n在[来源:java.io.PushbackInputStream@51ceaf1d; line:1,column:13]“}')
您可以在此处参考API信息:https://thingspace.verizon.com/developer/apis#/Connectivity%20Management/API%20Reference/Retrieve%20Device%20Information.html
我相信我的JSON字符串可能有问题,但我需要另一组眼睛。
答案 0 :(得分:1)
data
不会自动转换为json,您必须明确地执行此操作:
deviceList = requests.post(url, data=json.dumps(data), headers=headers)