Python requests library how to pass Authorization header with single token

后端 未结 8 1101
暗喜
暗喜 2020-12-02 14:02

I have a request URI and a token. If I use:

curl -s \"\" -H \"Authorization: TOK:\"

etc., I get a 200 and vie

相关标签:
8条回答
  • 2020-12-02 14:24

    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.

    0 讨论(0)
  • 2020-12-02 14:25

    This worked for me:

    access_token = #yourAccessTokenHere#
    
    result = requests.post(url,
          headers={'Content-Type':'application/json',
                   'Authorization': 'Bearer {}'.format(access_token)})
    
    0 讨论(0)
  • 2020-12-02 14:35

    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:

    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()
    
    0 讨论(0)
  • 2020-12-02 14:38

    You can try something like this

    r = requests.get(ENDPOINT, params=params, headers={'Authorization': 'Basic %s' %  API_KEY})
    
    0 讨论(0)
  • 2020-12-02 14:39

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

    To pass your own header you pass in a dictionary like so:

    r = requests.get('<MY_URI>', headers={'Authorization': 'TOK:<MY_TOKEN>'})
    
    0 讨论(0)
  • 2020-12-02 14:47

    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

    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:

    r=requests.get('<MY_URI', auth=('MY_USERNAME', ''))
    

    Hope this helps somebody :)

    0 讨论(0)
提交回复
热议问题