在我的API 7.2中,我不响应“我的API调用它的原因”

2024-10-02 10:18:31 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用请求从Python2.7调用SocialCastAPI时,我得到一个响应“reason”,但在实际的api中看不到该文本。我的代码是:

import requests
parameters = {"username" = "myUsername", "password" = "myPassword"}
response = requests.get("https://hub.sas.com/api/groups/808/messages.json", parameters)
response.json()

我要传递的JSON的开头是:

^{pr2}$

所以我希望有别的东西回来,但它的回报是:

{u'reason': u''}

这是一个错误还是有什么我不明白?在


Tags: 代码文本importapijsongetresponseusername
1条回答
网友
1楼 · 发布于 2024-10-02 10:18:31

我用这个代码解决了我的问题:

import requests
from requests.auth import HTTPBasicAuth
r = requests.get('https://hub.sas.com/api/groups/808/messages.json', auth=HTTPBasicAuth('username', 'password'))
data = r.json()
for message in data['messages']:
      print(message['user']['name'])

我不确定from requests.auth import HTTPBasicAuth或{}是否必要,但它最终对我有用,所以我把它们放在那里。在

相关问题 更多 >

    热门问题