【发布时间】:2014-11-05 23:50:28
【问题描述】:
我需要使用进行 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,
...
}
【问题讨论】:
-
确保您使用的是正确的项目 ID。此外,一般来说,如果后续问题无法放入评论中,您应该提出一个新问题。
-
我检查了,我的 project_id 是正确的。我尝试的一个有趣的事情是把这个网址放在浏览器中:pivotaltracker.com/services/v5/projects/my_project_id/epics/1 但这也返回了同样的错误
-
也许您没有 id 为 1 的史诗。使用 /services/v5/projects/my_project_id/epics 获取现有的史诗。
-
... D'OH 我以为它们只是按数字排序的。当然不是。感谢您提供的所有帮助并发现我的愚蠢错误。我很感激。
标签: python python-2.7 curl python-requests pivotaltracker