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
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'
You can encode the data and make the request by doing the following:
import requests, base64
usrPass = "userid:password"
b64Val = base64.b64encode(usrPass)
r=requests.post(api_URL,
headers={"Authorization": "Basic %s" % b64Val},
data=payload)
I'm not sure if you've to add the "BASIC" word in the Authorization field or not. If you provide the API link, It'd be more clear.
With python3, I have found a solution which is working for me:
import base64
userpass = username + ':' + password
encoded_u = base64.b64encode(userpass.encode()).decode()
headers = {"Authorization" : "Basic %s" % encoded_u}
As explained in the Requests documentation https://2.python-requests.org/en/latest/user/authentication/
Making requests with HTTP Basic Auth is very simple:
>>> from requests.auth import HTTPBasicAuth >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass')) <Response [200]>
In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand for using it:
>>> requests.get('https://api.github.com/user', auth=('user', 'pass')) <Response [200]>
Providing the credentials in a tuple like this is exactly the same as the HTTPBasicAuth example above.
I found "basicauth" package, it really made my that day. Using pip we can install.
pip install basicauth
Example client side code:
from flask import request
import basicauth
username = request.form['username']
passwd = request.form['password']
encoded_creds = basicauth.encode(username, passwd)
headers = {
"Authorization": "{0}".format(encoded_creds) # Replaces as "Authorization": "Basic WdfV0Adh4Kdf="
}
r = requests.post("http://10.0.0.1:8008/login"), headers=headers)
res = r.json()
print(res)
Example server side code
import basicauth
from flask import request
authorization = request.headers.get('Authorization')
if authorization is not None and "Basic " in authorization:
username, passwd = basicauth.decode(authorization)
print(username, passwd)
I recommend to use:
import request
auth = ('username', 'password')
r = requests.post(url, auth=auth)
Or
import request
from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth('username', 'password')
r = requests.post(url, auth=auth)
https://2.python-requests.org/en/master/user/authentication/#basic-authentication
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.