带有身份验证的python请求(access_token)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13825278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python request with authentication (access_token)
提问by user1895406
I am trying to get an API query into python. The command line
我正在尝试将 API 查询导入 python。命令行
curl --header "Authorization:access_token myToken" https://website.com/id
gives some json output. myToken is a hexadecimal variable that remains constant throughout. I would like to make this call from python so that I can loop through different ids and analyze the output. Any ideas? Before authentication was needed I have done that with urllib2. I have also taken a look at the requests module but couldn't figure out how to do that.
给出一些 json 输出。myToken 是一个十六进制变量,始终保持不变。我想从 python 进行这个调用,以便我可以遍历不同的 id 并分析输出。有任何想法吗?在需要身份验证之前,我已经使用 urllib2 完成了这项工作。我还查看了请求模块,但无法弄清楚如何做到这一点。
Many thanks.
非常感谢。
采纳答案by wosc
The requestspackage has a very nice API for HTTP requests, adding a custom header works like this (source: official docs):
该请求包有一个用于HTTP请求的一个非常好的API,加入了自定义标题是这样的(来源:官方文档):
>>> import requests
>>> response = requests.get(
... 'https://website.com/id', headers={'Authorization': 'access_token myToken'})
If you don't want to use an external dependency, the same thing using urllib2 of the standard library looks like this (source: the missing manual):
如果您不想使用外部依赖项,则使用标准库的 urllib2 的相同内容如下所示(来源:缺少的手册):
>>> import urllib2
>>> response = urllib2.urlopen(
... urllib2.Request('https://website.com/id', headers={'Authorization': 'access_token myToken'})
回答by bloodrootfc
I had the same problem when trying to use a token with Github.
尝试在 Github 中使用令牌时遇到了同样的问题。
The only syntax that has worked for me with Python 3 is:
在 Python 3 中对我有用的唯一语法是:
import requests
myToken = '<token>'
myUrl = '<website>'
head = {'Authorization': 'token {}'.format(myToken)}
response = requests.get(myUrl, headers=head)
回答by Orkhan M.
I'll add a bit hint: it seems what you pass as the key value of a header depends on your authorization type, in my case that was PRIVATE-TOKEN
我会添加一点提示:似乎您作为标头的键值传递的内容取决于您的授权类型,在我的情况下是 PRIVATE-TOKEN
header = {'PRIVATE-TOKEN': 'my_token'}
response = requests.get(myUrl, headers=header)
回答by tschmelz
Have you tried the uncurlpackage (https://github.com/spulec/uncurl)? You can install it via pip, pip install uncurl. Your curl request returns:
您是否尝试过该uncurl软件包(https://github.com/splec/uncurl)?您可以通过画中画安装它,pip install uncurl。您的 curl 请求返回:
>>> uncurl "curl --header \"Authorization:access_token myToken\" https://website.com/id"
requests.get("https://website.com/id",
headers={
"Authorization": "access_token myToken"
},
cookies={},
)
回答by Sowmiya Ragu
>>> import requests
>>> response = requests.get('https://website.com/id', headers={'Authorization': 'access_token myToken'})
If the above doesnt work , try this:
如果上述方法不起作用,请尝试以下操作:
>>> import requests
>>> response = requests.get('https://api.buildkite.com/v2/organizations/orgName/pipelines/pipelineName/builds/1230', headers={ 'Authorization': 'Bearer <your_token>' })
>>> print response.json()

