python执行curl,如何使用请求Python模块进行curl调用

I need to use an API that makes cURL calls. API shown here: https://www.pivotaltracker.com/help/api/rest/v5. I am coding in Python 2.7 and downloaded the Requests module to use for the cURL calls, however I'm not exactly sure how to do this. This is what I have so far:

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))

Now that I have the response r, what do I do with it to make the cURL calls in order to use the API functions, such as the GET /projects/{project_id}/epics/{epic_id} function. The cURL call for this function is:

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"

Thanks for any help you can provide!

EDIT (thanks to @Rob Watts)

Now this is my code:

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

But it still isn't working. This is the output:

{

"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."

}

But per the documentation, I expect something like this:

{

"created_at": "2014-08-26T12:00:00Z",

"description": "Identify the systems and eliminate the rebel scum.",

"id": 1,

...

}

解决方案

It looks like the first call you should make is to the '/me' endpoint, and then pull the API token from the response:

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']

You can get some other stuff besides your API token from that endpoint. Take a look at the documentation for that endpoint to see if there is anything else you will need.

Once you've gotten your API token, all the other calls will be fairly simple. For example:

project_id = 'your_project_id' # could get this from the previous response

r = requests.get('https://www.pivotaltracker.com/services/v5/projects/{}/epics/4'.format(project_id), headers={'X-TrackerToken':token})

I'll explain the parts of the cURL call they have for this example and how they translate:

export VARNAME

Set a variable for the cURL call to use. Where you see $VARNAME is where the variables are being used.

-X GET

I don't know why they include this. This just specifies to use a GET, which is the default for cURL. Using requests.get takes care of this. However, for ones that have -X POST, you'd use requests.post, etc.

-H "X-TrackerToken: $TOKEN"

This specifies a header. For Requests, we use the headers keyword argument - headers={key:value}. In this specific example, we have headers={'X-TrackerToken':token}.

"https://..."

The url. That goes in as the first argument. Variables (like $PROJECT_ID in your example) can be inserted using the format method of strings.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值