如何使用 Requests Python 模块进行 curl 调用 [英] How to use Requests Python module to make curl calls

查看:27
本文介绍了如何使用 Requests Python 模块进行 curl 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用进行 cURL 调用的 API.此处显示的 API:https://www.pivotaltracker.com/help/api/rest/v5.我正在使用 Python 2.7 进行编码并下载了用于 cURL 调用的 Requests 模块,但是我不确定如何执行此操作.这是我目前所拥有的:

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

现在我有了响应 r,我该怎么做才能进行 cURL 调用以使用 API 函数,例如 GET/projects/{project_id}/epics/{epic_id} 函数.此函数的 cURL 调用是:

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!

编辑(感谢@Rob Watts)现在这是我的代码:

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

推荐答案

看起来您应该首先调用 '/me' 端点,然后从响应中提取 API 令牌:

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

除了您的 API 令牌之外,您还可以从该端点获得一些其他东西.查看该端点的文档以查看是否有任何内容否则你将需要.

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.

获得 API 令牌后,所有其他调用都将非常简单.例如:

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

我将解释他们在此示例中的 cURL 调用部分以及它们如何翻译:

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

export VARNAME

为要使用的 cURL 调用设置一个变量.您看到 $VARNAME 的地方就是使用变量的地方.

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

-X GET

我不知道他们为什么包含这个.这只是指定使用 GET,这是 cURL 的默认值.使用 requests.get 可以解决这个问题.但是,对于具有 -X POST 的那些,您可以使用 requests.post 等.

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"

这指定了一个标题.对于请求,我们使用 headers 关键字参数 - headers={key:value}.在这个特定的例子中,我们有 headers={'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://..."

网址.这是第一个论点.可以使用字符串的 format 方法插入变量(如示例中的 $PROJECT_ID).

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.

这篇关于如何使用 Requests Python 模块进行 curl 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆