Search code examples
pythonpython-requeststokenbasic-authenticationrequest-headers

Authorization missing error when using the token as header using python requests module


Trying to convert curl below to the Python requests, below is the curl command which is working fine.

curl -k -H "Authorization: Token token=\"$token\""https://conjur.com/secret

Above curl works fine and gives expected output but when I turn that into python requests it is giving me trouble the header is weird not sure how to pass that, i tried below

token="abcdef"
token_header = {'Authorization': f'Token token={token}'}
requests.get("https://conjur.com/secret", headers=token_header).text


Above code gives error and looks like header is not working as expected,let me know how can i solve it ?

Already mentioned above


Solution

  • In your curl command you have quotes around the token:

    curl -k -H "Authorization: Token token=\"$token\"" https://conjur.com/secret
    

    Note the token=\"$token\"


    To have quotes in your Python code do:

    token_header = {"Authorization": f'Token token="{token}"'}