[英]Python requests library how to pass Authorization header with single token
I have a request URI and a token.我有一个请求 URI 和一个令牌。 If I use:
如果我使用:
curl -s "<MY_URI>" -H "Authorization: TOK:<MY_TOKEN>"
etc., I get a 200 and view the corresponding JSON data.等等,我得到一个200并查看相应的JSON数据。 So, I installed requests and when I attempt to access this resource I get a 403 probably because I do not know the correct syntax to pass that token.
因此,我安装了请求,当我尝试访问此资源时,我得到 403,可能是因为我不知道传递该令牌的正确语法。 Can anyone help me figure it out?
谁能帮我弄清楚吗? This is what I have:
这就是我所拥有的:
import sys,socket
import requests
r = requests.get('<MY_URI>','<MY_TOKEN>')
r. status_code
I already tried:我已经尝试过:
r = requests.get('<MY_URI>',auth=('<MY_TOKEN>'))
r = requests.get('<MY_URI>',auth=('TOK','<MY_TOKEN>'))
r = requests.get('<MY_URI>',headers=('Authorization: TOK:<MY_TOKEN>'))
But none of these work.但这些都不起作用。
In python:在蟒蛇中:
('<MY_TOKEN>')
is equivalent to相当于
'<MY_TOKEN>'
And requests interprets并请求解释
('TOK', '<MY_TOKEN>')
As you wanting requests to use Basic Authentication and craft an authorization header like so:当您希望请求使用基本身份验证并制作授权标头时,如下所示:
'VE9LOjxNWV9UT0tFTj4K'
Which is the base64 representation of 'TOK:<MY_TOKEN>'
这是
'TOK:<MY_TOKEN>'
的 base64 表示
To pass your own header you pass in a dictionary like so:要传递您自己的标题,您需要传入一个字典,如下所示:
r = requests.get('<MY_URI>', headers={'Authorization': 'TOK:<MY_TOKEN>'})
I was looking for something similar and came across this .我正在寻找类似的东西并遇到了 这个。 It looks like in the first option you mentioned
看起来像你提到的第一个选项
r = requests.get('<MY_URI>', auth=('<MY_TOKEN>'))
"auth" takes two parameters: username and password, so the actual statement should be “auth”有两个参数:用户名和密码,所以实际语句应该是
r=requests.get('<MY_URI>', auth=('<YOUR_USERNAME>', '<YOUR_PASSWORD>'))
In my case, there was no password, so I left the second parameter in auth field empty as shown below:就我而言,没有密码,所以我将 auth 字段中的第二个参数留空,如下所示:
r=requests.get('<MY_URI', auth=('MY_USERNAME', ''))
Hope this helps somebody :)希望这对某人有所帮助:)
This worked for me:这对我有用:
access_token = #yourAccessTokenHere#
result = requests.post(url,
headers={'Content-Type':'application/json',
'Authorization': 'Bearer {}'.format(access_token)})
You can also set headers for the entire session:您还可以为整个会话设置标题:
TOKEN = 'abcd0123'
HEADERS = {'Authorization': 'token {}'.format(TOKEN)}
with requests.Session() as s:
s.headers.update(HEADERS)
resp = s.get('http://example.com/')
i founded here, its ok with me for linkedin: https://auth0.com/docs/flows/guides/auth-code/call-api-auth-code so my code with with linkedin login here:我在这里成立,它可以与我一起使用linkedin: https : //auth0.com/docs/flows/guides/auth-code/call-api-auth-code所以我的代码与linkedin登录:
ref = 'https://api.linkedin.com/v2/me'
headers = {"content-type": "application/json; charset=UTF-8",'Authorization':'Bearer {}'.format(access_token)}
Linkedin_user_info = requests.get(ref1, headers=headers).json()
Requests natively supports basic auth only with user-pass params, not with tokens.请求本身仅支持用户传递参数的基本身份验证,而不支持令牌。
You could, if you wanted, add the following class to have requests support token based basic authentication:如果需要,您可以添加以下类以使请求支持基于令牌的基本身份验证:
import requests
from base64 import b64encode
class BasicAuthToken(requests.auth.AuthBase):
def __init__(self, token):
self.token = token
def __call__(self, r):
authstr = 'Basic ' + b64encode(('token:' + self.token).encode('utf-8')).decode('utf-8')
r.headers['Authorization'] = authstr
return r
Then, to use it run the following request :然后,要使用它运行以下请求:
r = requests.get(url, auth=BasicAuthToken(api_token))
An alternative would be to formulate a custom header instead, just as was suggested by other users here.另一种方法是制定自定义标题,正如其他用户在这里建议的那样。
你可以试试这样的
r = requests.get(ENDPOINT, params=params, headers={'Authorization': 'Basic %s' % API_KEY})
This worked for me:这对我有用:
r = requests.get('http://127.0.0.1:8000/api/ray/musics/', headers={'Authorization': 'Token 22ec0cc4207ebead1f51dea06ff149342082b190'})
My code uses user generated token.我的代码使用用户生成的令牌。
You have a request needing an authorization maybe you have a result 401
.您有一个需要授权的请求,也许您有一个结果
401
。
Suppose your request is like this :假设您的请求是这样的:
REQ ='https://api.asite.com/something/else/else'
You have your token :你有你的令牌:
TOKEN = 'fliuzabuvdgfnsuczkncsq12454632'
build your header like this :像这样构建你的标题:
HEADER = {'Authorization': f'{TOKEN}'}
and use it like this :并像这样使用它:
req.get(REQ, headers=HEADER)
display your result like this :像这样显示你的结果:
req.get(COACH, headers=HEADER).json()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.