import urllib.request
import urllib.error
import urllib.parse
import xml.dom.minidom
from xml.dom.minidom import parse, parseString

key = "3ac024abf8c72460764b965199f81808f4cb08"
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><agendize><account><campaign><name>Campagne1</name><color>#EC50FF</color><description>This is a campaign uploaded by python</description></campaign></account></agendize>"
theurl = "http://www.agendize.com/api/1.0/data"

# Build password manager by setting th parameter "realm" as default
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
# Encode the parameters in order to build a suitable query
queryEncoded = urllib.parse.urlencode({'key': key, 'xml': xml})
# Add a passwword to our password manager
password_mgr.add_password(None, theurl, "myusername", "mypassword")
# Create our HTML request manager by activing authentication
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
# Build the opener using our request manager
opener = urllib.request.build_opener(handler)
# Install the opener to be efficient
urllib.request.install_opener(opener)
req = urllib.request.Request(url=theurl,data=str.encode(queryEncoded))
# Make the connection 
handle = urllib.request.urlopen(req)
# Convert in string
chaineResultat = bytes.decode(handle.read())

# Retrieve AgendiZe response code obtained by searching in the parsed xml content
dom = parseString(chaineResultat)
element = dom.getElementsByTagName("result")[0]
print(element.childNodes[0].data)