【发布时间】:2016-06-23 08:22:44
【问题描述】:
我正在尝试使用 python 的请求来实现 Synchroteam API 的基本实现。我能够使用 cURL 作为概念证明建立连接并获取我想要的所有数据,但是使用请求我可以使用我的凭据进入该站点,但我得到的只是:
500
text/html; charset=utf-8
https://apis.synchroteam.com/Api/v1/user/list
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Error</title>
</head>
<body>
<h2>
An error occurred while processing your request.
</h2>
</body>
</html>
Process finished with exit code 0
当在这一行中使用 curl 时,一切都会返回正确的信息:
curl -u domain:APIKEY -H "Accept: application/json" -H "Content-Type: application/json" https://apis.synchroteam.com/Api/v1/user/List
我的一小段python代码是这样的:
import requests
apiurl = 'https://apis.synchroteam.com/Api/v1/user/list'
login = ("domain", "APIKEY")
headers = {'Content-Type': 'application/json'}
def run():
r = requests.get(apiurl, auth=login, headers=headers)
print r.status_code
print r.headers['Content-type']
print r.url
print r.text
run()
所有的 Synchroteam 文档都有 PHP 示例,但我看不出有什么理由不能使用 python。 文档在这里:http://api.synchroteam.com/rest.php
任何见解都会很棒,我真的希望它只是我缺少的一些小东西。 谢谢。
【问题讨论】:
-
您是否也尝试过设置
Accept:标头? -
我没有。知道我应该将其设置为接受什么吗?我不确定它目前挂断了什么。很抱歉这个基本问题,这是我第一次使用任何 HTTP 或外部 API
-
更新:我没有试过这个。我现在意识到这类似于 Curl 线。非常感谢,这已经困扰了我好几个小时了。
标签: php python curl python-requests