Spark Core的API返回HTTPS 400错误

时间:2015-04-16 18:53:13

标签: python http https

我正在尝试向Spark的Cloud API发出HTTPS请求。我在httplib库中使用以下代码

import httplib
spark = httplib.HTTPSConnection("api.spark.io")
spark.request("GET", "/v1/devices")
response = spark.getresponse()
print response.status, response.reason

根据Spark API documentation,这应该有效。但相反,我收到400 Bad Request。我究竟做错了什么?

2 个答案:

答案 0 :(得分:2)

Spark Cloud使用传统的HTTP响应代码来提供来自Core的有关请求及其成功或失败的有效性的反馈。

您收到400错误的原因是您没有发布access tokenid,如以下文档中的示例所示:

POST /v1/devices

# EXAMPLE REQUEST
curl https://api.spark.io/v1/devices \
     -d access_token=1234 \
     -d id=0123456789abcdef01234567

使用此修复程序可以轻松解决此问题:

import httplib
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
params = {"access_token": "1234", "id": "0123456789abcdef01234567"}
spark = httplib.HTTPSConnection("api.spark.io")
spark.request("GET", "/v1/devices", params, headers)
response = spark.getresponse()
print response.status, response.reason

参数应该是您的access tokenid

答案 1 :(得分:1)

忘记阅读回复内容?

>>> import httplib
>>> spark = httplib.HTTPSConnection("api.spark.io")
>>> spark.request("GET", "/v1/devices")
>>> response = spark.getresponse()
>>> print response.status, response.reason
400 Bad Request
>>> response.read()
'{\n  "error": "invalid_request",\n  "error_description": "The access token was not found"\n}'
>>> 

FWIW您可以自己发现只需访问浏览器中的网址...