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
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.
b64encode() in Python. With the help of base64. b64encode() method, we can encode the string into the binary form. Return : Return the encoded string.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With