Using my code below, sometimes the header splits onto two lines and puts an extra set of singles quotes:
{'Authorization': 'Basic '
'bmVhbHdhbHRlcnNAZ21haWwuY29tOm9xRUh4UFVwaHlnY25AOCNAI15hTUI=',
'accept': 'application/json'}
But what I’m expecting is :
{‘Authorization’: ‘Basic bmVhbHdhbHRlcnNAZ21haWwuY29tOnNpbXBsZQ==’,
‘accept’: ‘application/json’}
I’m guessing it might be some encoding issue.
I’m trying to match my headers to a Swagger test page that allows me to execute the logon. It only shows the CURL post, so I’m trying to make sure my headers exactly match what is shown with the curl, because I keep getting login failures when do request.post in Python.
Also if I pass a user like ‘nealwalters’ instead of ‘[email protected]’ it comes out on one line as well.
Sample code to reproduce with Python 3:
import requests
import pprint
import base64
from base64 import b64encode
#from requests.auth import HTTPBasicAuth
# Function from /questions/6999565/python-https-get-with-basic-authentication
# added 6/5/2024
# Authorization token: we need to base 64 encode it
# and then decode it to acsii as python 3 stores it as a byte string
def basic_auth(username, password):
token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii")
#token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ISO-8859-1")
return f'Basic {token}'
# new function added 05/31/2024
def check_proxy_usage():
admin_user = '[email protected]' # not real
# admin_password = 'mypassword' # rerun with the password below to see issue
admin_password = 'oqEHxPUphygcn@8#@#^aXY'
#credentials = base64.b64encode(b'{user}:{password}').decode('utf-8')
user_pass = f'{admin_user}:{admin_password}'
print(user_pass)
#credentials = base64.b64encode(bytes(user_pass, 'utf-8'))
credentials = base64.b64encode(bytes(user_pass, 'utf-8')).decode('utf-8')
print("credentials=" + str(credentials));
headers_1 = {
'accept': 'application/json',
'Authorization': f'Basic {credentials}',
}
print("headers_1:")
pprint.pprint (headers_1)
basic_auth_str_1 = basic_auth(admin_user, admin_password)
print("basic_auth_str_1:", basic_auth_str_1)
basic_auth_str_2 = 'Basic ' + credentials
print("basic_auth_str_2:", basic_auth_str_2)
headers_2 = {
'accept': 'application/json',
'Authorization': basic_auth_str_2
}
headers_json = {
'accept': 'application/json'
}
print("headers_2:")
pprint.pprint (headers_2)
if __name__ == "__main__":
check_proxy_usage()
Output with complex password:
[email protected]:oqEHxPUphygcn@8#@#^aMB
credentials=bmVhbHdhbHRlcnNAZ21haWwuY29tOm9xRUh4UFVwaHlnY25AOCNAI15hTUI=
headers_1:
{'Authorization': 'Basic '
'bmVhbHdhbHRlcnNAZ21haWwuY29tOm9xRUh4UFVwaHlnY25AOCNAI15hTUI=',
'accept': 'application/json'}
basic_auth_str_1: Basic bmVhbHdhbHRlcnNAZ21haWwuY29tOm9xRUh4UFVwaHlnY25AOCNAI15hTUI=
basic_auth_str_2: Basic bmVhbHdhbHRlcnNAZ21haWwuY29tOm9xRUh4UFVwaHlnY25AOCNAI15hTUI=
headers_2:
{'Authorization': 'Basic '
'bmVhbHdhbHRlcnNAZ21haWwuY29tOm9xRUh4UFVwaHlnY25AOCNAI15hTUI=',
'accept': 'application/json'}
Output with more simple user/password:
[email protected]:simple
credentials=bmVhbHdhbHRlcnNAZ21haWwuY29tOnNpbXBsZQ==
headers_1:
{'Authorization': 'Basic bmVhbHdhbHRlcnNAZ21haWwuY29tOnNpbXBsZQ==',
'accept': 'application/json'}
basic_auth_str_1: Basic bmVhbHdhbHRlcnNAZ21haWwuY29tOnNpbXBsZQ==
basic_auth_str_2: Basic bmVhbHdhbHRlcnNAZ21haWwuY29tOnNpbXBsZQ==
headers_2:
{'Authorization': 'Basic bmVhbHdhbHRlcnNAZ21haWwuY29tOnNpbXBsZQ==',
'accept': 'application/json'}