Failed to decode basic authentication token Python 3.10

Viewed 59

I am creating script to send calls to API endpoints via simple python functions. I have the following function to retrieve my OAuth2 token (I will remove the URLs and credentials and nevermind the padding)

def get_new_token():
auth_server_url = "my auth url here"
client_id = "my client id here"
client_secret = "my secret here"
auth_headers = {'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'}
token_req_payload = {'grant_type': 'client_credentials'}
token_response = requests.post(auth_server_url,
                           data=token_req_payload, verify=False, allow_redirects=False,
                           auth=(client_id, client_secret), headers=auth_headers)

I am then successfully retrieving token with the following line:

return token_response.json()["access_token"]

Then, I can successfully make a request to one of the endpoints, here is my function (also, nevermind the padding):

def call_api_v1_accounts(token):
api_call_headers = {
                    'Content-Length': '10204',
                    'Content-Type': 'application/json',
                    'User-Agent': 'removed',
                    'Authorization': 'Bearer ' + token}

data = {"here": "is my payload"}
api_call_response = requests.post(my_url, headers=api_call_headers, data=json.dumps(data), verify=False)
print(api_call_response.text)

This request go through fine and I retrieve the information I need, here is the sample of response (I also removed actual data due to security reasons)

{"timestamp":"2022-08-09T10:24:29.713Z","account":{"id":"omitted"},"web":{"href":"omitted"},"sdk":{"token":"omitted"},"workflowExecution":{"id":"omitted","credentials":[{"id":"omitted","category":"ID","omitted":["WEB","API","SDK"],"api":{"token":"ommitted]}]}}

Here is my problem, finally. I have another function which uses the same token, function is below (nevermind the padding)

def call_api_netverify_acquisitions(token):
api_call_headers = {
    'Content-Length': '10204',
    'Content-Type': 'application/json',
    'User-Agent': 'removed',
    'Authorization': 'Basic ' + token}
data = {'mydata':'ishere'}
api_call_response = requests.post(my_url, headers=api_call_headers, data=json.dumps(data), verify=True)
print(api_call_response.text)

And the response I receive with it is the following:

{"message":"Failed to decode basic authentication token.","httpStatus":401,"requestUri":"endpoint_url_here"}

I really have no idea what's wrong with that? I printed the len(token) and it returned 867 so I decided I need to add one more symbol to that (so it can be divided by 4), I've added "=" or "===" or "=======" and the result is the same. I understand that one function uses 'Bearer' and other one uses 'Basic', but I still have no idea what to look for.

I am really struggling, please help.

0 Answers
Related