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

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

问题描述

我需要使用一个可以进行cURL调用的API。此处显示的API: https://www.pivotaltracker.com/help/api/rest/v5 。我在Python 2.7编码,并下载了Requests模块用于cURL调用,但我不知道如何做到这一点。这是我到目前为止:

 导入请求

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 ='您的Pivotal Tracker API令牌'
export PROJECT_ID = 99

curl -X GET -HX-TrackerToken:$ TOKENhttps://www.pivotaltracker.com/services/v5/projects/$PROJECT_ID/epics/4
感谢您提供任何帮助!



> EDIT (感谢@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


b $ b

但它仍然不工作。这是输出:

  {
code:unfound_resource,
kind error,
error:无法找到您尝试访问的对象。可能已被另一个用户删除,您可能正在使用另一个对象类型的ID,或者您可能尝试访问树中错误点处的子资源。
}

但是根据文档,我期望这样:

  {
created_at:2014-08-26T12:00:00Z,
description:系统并消除叛乱败类。
id:1,
...
}


解决方案

看起来你应该做的第一个调用是'/ me'端点,然后从响应中拉取API令牌: / p>

 导入请求

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令牌外。请查看该端点的文档,看看是否有任何内容



一旦你获得了你的API令牌,所有其他的调用都会相当简单。例如:

  project_id ='your_project_id'#可以从上一个响应中获得这个
r = requests.get https://www.pivotaltracker.com/services/v5/projects/{}/epics/4'.format(project_id),headers = {'X-TrackerToken':token})

我将解释这个例子中cURL调用的部分以及它们的翻译方式:

  export VARNAME 

cURL调用使用。 $ VARNAME 是使用变量的位置。

  -X GET 

我不知道为什么他们包括这个。这只是指定使用GET,这是cURL的默认值。使用 requests.get 来处理这个问题。但是,对于有 -X POST 的用户,您可以使用 requests.post 等。

  -HX-TrackerToken:$ TOKEN

这指定了一个头。对于请求,我们使用 headers 关键字参数 - headers = {key:value} 。在此特定示例中,我们有 headers = {'X-TrackerToken':token}

 https:// ...

这是第一个参数。可以使用字符串的格式方法插入变量(例如 $ PROJECT_ID )。


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.

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

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