Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base64 Authentication Python

I'm following an api and I need to use a Base64 authentication of my User Id and password.

'User ID and Password need to both be concatenated and then Base64 encoded'

it then shows the example

'userid:password' 

It then proceeds to say 'Provide the encoded value in an "Authorization Header"'

'for example: Authorization: BASIC {Base64-encoded value}'

How do I write this into a python api request?

z = requests.post(url, data=zdata ) 

Thanks

like image 262
Marcus Avatar asked Aug 09 '13 01:08

Marcus


People also ask

What is Base64 authentication?

Basic Authentication sends a Base64 encoded string that contains a user name and password for the client via HTTP headers. Base64 is not a form of encryption and should be considered the same as sending the user name and password in clear text. However, all traffic is encrypted and transmitted over a TLS v1.

What is Base64 b64encode in Python?

b64encode() in Python. With the help of base64. b64encode() method, we can encode the string into the binary form. Return : Return the encoded string.


1 Answers

The requests library has Basic Auth support and will encode it for you automatically. You can test it out by running the following in a python repl

from requests.auth import HTTPBasicAuth r = requests.post(api_URL, auth=HTTPBasicAuth('user', 'pass'), data=payload) 

You can confirm this encoding by typing the following.

r.request.headers['Authorization'] 

outputs:

u'Basic c2RhZG1pbmlzdHJhdG9yOiFTRG0wMDY4' 
like image 89
Josh Avatar answered Sep 21 '22 12:09

Josh