return 字符或 header 中的前导 space 无效:授权 // - Python

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

所以我有一个尝试执行函数调用的方法,整个过程需要一个 base64 编码的密钥和秘密

我有一个 class 方法,它正在尝试做正确的事情并进行调用,但我遇到了错误

我的方法如下:

# 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"})

而 return 上的错误是

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

我已经尝试了各种方法来做所需的事情,但我觉得字符串周围的 b'' 是问题所在,因此我解码为 UTF-8。

非常欢迎任何建议

造成您麻烦的原因是 base64.encodestring 的使用。根据文档,它将数据拆分为 base64 数据的 ,因此存在换行符。

更喜欢直接使用 b64encodeb64decode,他们不会做这个魔法

## 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"})