我需要使用进行 cURL 调用的 API。此处显示的 API:https ://www.pivotaltracker.com/help/api/rest/v5 。我在 Python 2.7 中编码并下载了 Requests 模块以用于 cURL 调用,但是我不确定如何执行此操作。这是我到目前为止所拥有的:
import requests
username = 'my_username'
password = 'my_password'
url = 'https://www.pivotaltracker.com/n/projects/my_project_number'
r = requests.get(url, auth=(username, password))
现在我有了响应 r,我该如何处理它来进行 cURL 调用以使用 API 函数,例如 GET /projects/{project_id}/epics/{epic_id} 函数。此函数的 cURL 调用是:
export TOKEN='your Pivotal Tracker API token'
export PROJECT_ID=99
curl -X GET -H "X-TrackerToken: $TOKEN" "https://www.pivotaltracker.com/services/v5/projects/$PROJECT_ID/epics/4"
感谢您的任何帮助,您可以提供!
编辑(感谢@Rob Watts)现在这是我的代码:
import requests
username = 'my_username'
password = 'my_password'
url = 'https://www.pivotaltracker.com/services/v5/me'
r = requests.get(url, auth=(username, password))
response_json = r.json()
token = response_json['api_token']
project_id = 'my_project_id'
url = 'https://www.pivotaltracker.com/services/v5/projects/{}/epics/1'
r = requests.get(url.format(project_id), headers={'X-TrackerToken':token})
print r.text
但它仍然无法正常工作。这是输出:
{
"code": "unfound_resource",
"kind": "error",
"error": "The object you tried to access could not be found. It may have been removed by another user, you may be using the ID of another object type, or you may be trying to access a sub-resource at the wrong point in a tree."
}
但是根据文档,我希望是这样的:
{
"created_at": "2014-08-26T12:00:00Z",
"description": "Identify the systems and eliminate the rebel scum.",
"id": 1,
...
}