我正在尝试从python 2.7转换为3.4并尝试使用b64encode时出现以下错误:

from base64 import b64encode

username = 'manager'
password = 'test.manager'

headers = {"Authorization": " Basic " + b64encode(username + ":" + password), "Content-Type": "application/json"}


错误:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\python34\Lib\base64.py", line 62, in b64encode
    encoded = binascii.b2a_base64(s)[:-1]
**TypeError: 'str' does not support the buffer interface**

最佳答案

使用bytes对象代替:

username = b'manager'
password = b'test.manager'

headers = {"Authorization": b" Basic " + b64encode(username + b":" + password), "Content-Type": "application/json"}

10-04 21:47