Reputation: 21
My scripting skills are very basic. I'm trying to adapt a Python 2 snippet I found on here to Python 3.
I'm having difficulty formatting this line (I understand it needs a bytes-object but can't get it to work).
headers["Authorization"] = "Basic {0}".format(
base64.b64encode("{0}:{1}".format('auth', 'login')))
Full snippet:
import base64, http.client
headers = {}
body = '/api/2.1/xml-in'
headers["Authorization"] = "Basic {0}".format(
base64.b64encode("{0}:{1}".format('auth', 'login')))
headers["Content-type"] = "application/xml"
# the XML we ll send to Freshbooks
XML = """<?xml version="1.0" encoding="utf-8"?>
<request method="task.list">
<page>1</page>
<per_page>15</per_page>
</request>"""
# Enable the job
conn = http.client.HTTPSConnection('sample.freshbooks.com')
conn.request('POST', body, None, headers)
resp = conn.getresponse()
print(resp.status)
conn.send(XML)
print(resp.read())
conn.close()
I've tried the following but then get an error about formatting the str:
headers["Authorization"] = "Basic {0}".format(
base64.b64encode("%b:%b" % b'auth', b'login'))
Any help would be greatly appreciated. Thanks.
Upvotes: 0
Views: 166
Reputation: 21
It's a bit of a mouthful but the following works:
headers["Authorization"] = "Basic {0}".format(base64.b64encode(("{0}:{1}".format('string', 'string').encode('utf-8'))).decode())
Upvotes: 1