Home > Blockchain >  Invalid return character or leading space in header: Authorization // - Python
Invalid return character or leading space in header: Authorization // - Python

Time:02-15

So I have a method trying to do a function call and the whole thing requires a base64 encoded key and secret

I have a class method thats trying to so the right stuff and make the call but im getting an error

My method as follows:

# Build the connection string
        connection_string = f'{self.org_id}:{rr_key}:{rr_secret}'
        connection_string = connection_string.encode("ascii")
        logging.info(f'Connection string to be encoded: {connection_string}')

        # Base 64 encode the file
        base64encode = base64.encodestring(connection_string)
        logging.info(f'Base 64 string to send: Basic {base64encode.decode("utf-8")}')

        # Fetch the token from API
        encoded_string = base64encode.decode("utf-8")
        encoded_string = encoded_string.replace('\r', '')
        fetch_token = requests.post(f'{self.url}/oauth/token',
                                    headers={'Authorization': f'Basic {encoded_string}'},
                                    data={"grant_type": "client_credentials"})

And the error on return is

File "/root/.local/share/virtualenvs/kojo-backend-app-tMeByA0R/lib/python3.8/site-packages/requests/sessions.py", line 515, in request
    prep = self.prepare_request(req)
  File "/root/.local/share/virtualenvs/kojo-backend-app-tMeByA0R/lib/python3.8/site-packages/requests/sessions.py", line 443, in prepare_request
    p.prepare(
  File "/root/.local/share/virtualenvs/kojo-backend-app-tMeByA0R/lib/python3.8/site-packages/requests/models.py", line 319, in prepare
    self.prepare_headers(headers)
  File "/root/.local/share/virtualenvs/kojo-backend-app-tMeByA0R/lib/python3.8/site-packages/requests/models.py", line 453, in prepare_headers
    check_header_validity(header)
  File "/root/.local/share/virtualenvs/kojo-backend-app-tMeByA0R/lib/python3.8/site-packages/requests/utils.py", line 1025, in check_header_validity
    raise InvalidHeader("Invalid return character or leading space in header: %s" % name)
requests.exceptions.InvalidHeader: Invalid return character or leading space in header: Authorization

I have tried various ways of doing whats needed but I feel the b'' around the string is the issue hence why i decoded to UTF-8.

Any suggestions would be super welcome

CodePudding user response:

The cause of your troubles is the use of base64.encodestring. According to the documentation, it'll split the data in lines of base64 data, hence the presence of the newline characters.

Prefer using b64encode et b64decode directly, they do not do this magic

## Preamble to reproduce the problem
import logging
import base64
import requests

org_id = "toto_org"
rr_key = "key"
rr_secret = "secret"

# Build the connection string
connection_string = f'{org_id}:{rr_key}:{rr_secret}'
connection_string = connection_string.encode("ascii")
logging.info(f'Connection string to be encoded: {connection_string}')

# Base 64 encode the file
## ! I changed the method used here
base64encode = base64.b64encode(connection_string)
logging.info(f'Base 64 string to send: Basic {base64encode.decode("utf-8")}')

# Fetch the token from API
# ! ascii codec is sufficient here, your data is in base64 format
encoded_string = base64encode.decode('ascii')

fetch_token = requests.post(f'http://localhost/oauth/token',
                        headers={'Authorization': f'Basic {encoded_string}'},
                        data={"grant_type": "client_credentials"})
  • Related