How to use Requests Python module to make curl cal

2019-02-16 05:54发布

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,
    ...
}

1条回答
来,给爷笑一个
2楼-- · 2019-02-16 06:22

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.

查看更多
登录 后发表回答