1
import urllib, requests, http.client
user = ""
pw = ""
apiurl = "https://webservices6.autotask.net/atservices/1.5/atws.wsdl"
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
top_level_url = "https://webservices6.autotask.net/"
password_mgr.add_password(None, top_level_url, user, pw)
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
opener = urllib.request.build_opener(handler)
opener.open(apiurl)
urllib.request.install_opener(opener)
page = urllib.request.urlopen(apiurl)
SM_TEMPLATE ="""<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="https://webservices6.autotask.net/atservices/1.5/atws.wsdl" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<queryxml>
<entity>Contact</entity>
<query>
<condition>
<field>phone<expression op='equals'>#{phone}</expression></field>
</condition>
</query>
</queryxml>
</env:Body>
</env:Envelope>"""
SoapMessage = SM_TEMPLATE%()
# construct and send the header
webservice = http.client.HTTPSConnection("webservices6.autotask.net", 443)
webservice.putrequest("POST", "/atservices/1.5/atws.wsdl")
webservice.putheader("Host", "webservices6.autotask.net")
webservice.putheader("User-Agent", "Python post")
webservice.putheader("Content-type", "application/soap+xml; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(SoapMessage))
webservice.putheader("SOAPAction", "http://autotask.net/ATWS/v1_5/query")
webservice.endheaders()
webservice.send(SoapMessage.encode('utf-8'))
# get the response
statusmessage = webservice.getresponse()
print ("Response: ", statusmessage.read())
我正在寫這個來使用提供的API在Autotask上查詢,修改實體。但是,當我運行代碼時,出現此錯誤:在Python 3上使用API訪問Autotask
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Header</h2>
<hr><p>HTTP Error 400. The request has an invalid header name.</p>
</BODY></HTML>