Dilli Dilli - 1 month ago 8
Python Question

How would I pass in authtoken in my browser get Requests

How would I make following requests

$ curl -u eyJhbGciOiJIUzI1NiIsImV4cCI6MTM4NTY2OTY1NSwiaWF0IjoxMzg1NjY5MDU1fQ.eyJpZCI6MX0.XbOEFJkhjHJ5uRINh2JA1BPzXjSohKYDRT472wGOvjc:unused -i -X GET http://127.0.0.1:5000/api/resource
HTTP/1.0 200 OK
Content-Type: application/json


Work on my python-get requests without having to use my login information but just my auth token like how I made it working on curl?

I tried the following code it throws me
unauthorized access


response = requests.get('http://127.0.0.1:5000/api/resource', headers={'Authorization':'eyJhbGciOiJIUzI1NiIsImV4cCI6MTQ3Nzc3MTE2MiwiaWF0IjoxNDc3NzcwNTYyfQ.eyJpZCI6MX0.3IbZmxhDXxymOzXBKnv-rbMDBV_nFYaf-IWIW5YBnWE:unused'})
print response.text

Answer

In the curl manpage:

-u, --user <user:password>
    Specify the user name and password to use for server authentication.

In the requests documentation requests documentation:

from requests.auth import HTTPBasicAuth
requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))

So the following might work (untested):

from requests.auth import HTTPBasicAuth
response = requests.get('http://127.0.0.1:5000/api/resource', auth=HTTPBasicAuth('eyJhbGciOiJIUzI1NiIsImV4cCI6MTQ3Nzc3MTE2MiwiaWF0IjoxNDc3NzcwNTYyfQ.eyJpZCI6MX0.3IbZmxhDXxymOzXBKnv-rbMDBV_nFYaf-IWIW5YBnWE','unused')
print response.text
Comments