【发布时间】:2020-01-02 09:12:33
【问题描述】:
我想通过 API 从这个 url https://api.hooktheory.com/v1/users/auth 获取一些数据
我可以用键输入,但是一旦我想请求一些数据它就不起作用,因为以下错误。
我已经通过https://bootstrap.pypa.io/get-pip.py 下载了 pip 并导入了请求,我也通过 conda install pip 进行了尝试。没有任何效果,问题仍然存在。我已经在这里搜索了一些解决方案,但是它。不是重复的。仅供参考:我在带有 Visual Studio 的 Mac OS X 上工作。
import requests
import time
login = {"Accept": "application/json",
"Content-Type": "application/json",
"username":"huks",
"password": "XXXX"}
url = "https://api.hooktheory.com/v1/users/auth"
r = requests.post(url, data=login)
print(r.json())
time.sleep(5)
activkey = 'XXXX'
header = {"Authorization": "Bearer " + activkey}
r = requests.get(url+'trends/songs', headers=header)
r.json()
r = requests.get(url+'trends/nodes?cp=4', headers=header)
r.json()
这里是回溯+错误信息:
File "/Users/marius/Desktop/INNOLAB/tempCodeRunnerFile.py", line 20, in <module>
r.json()
File "/Users/marius/anaconda3/lib/python3.7/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/Users/marius/anaconda3/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/Users/marius/anaconda3/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/marius/anaconda3/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
【问题讨论】:
-
查看
r.status_code和r.text。这可能是因为您的请求有问题,响应不是 JSON。 -
检查您的 API 链接,我认为您在这里遇到了问题:
.../authtrends/songs添加时:url+'trends/songs' -
Accept & Content-Type are headers not payload!?
-
@pako for r.text 发生 TypeError: 'str' object is not callable。并且 r.status_code 是 200
-
@LêTưThành API 链接和手册在此处提到:hooktheory.com/api/trends/docs
标签: python