Asp.net web api 在Python 3上使用API访问自动任务

Asp.net web api 在Python 3上使用API访问自动任务,asp.net-web-api,python-3.4,Asp.net Web Api,Python 3.4,我写这篇文章是为了使用提供的API查询、修改自动任务上的实体。但是,当我运行这些代码时,出现了以下错误: 错误的请求 错误的请求-无效的标头 HTTP错误400。请求的标头名称无效 我有一个SUDS库,可以在我的项目中集成自动任务SOAP api 我想你错过了肥皂信封的标题标签。当我直接调用API函数时,使用SUDS库形成的信封。请尝试更正您的信封,如下所示: import urllib, requests, http.client user = "" pw = "" apiurl = "htt

我写这篇文章是为了使用提供的API查询、修改自动任务上的实体。但是,当我运行这些代码时,出现了以下错误:

错误的请求 错误的请求-无效的标头 HTTP错误400。请求的标头名称无效


我有一个SUDS库,可以在我的项目中集成自动任务SOAP api

我想你错过了肥皂信封的标题标签。当我直接调用API函数时,使用SUDS库形成的信封。请尝试更正您的信封,如下所示:

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())
如果您需要肥皂水样品,请随时联系我

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://autotask.net/ATWS/v1_5/" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns1:Body>
      <ns0:query>
         <ns0:sXML>
        <queryxml version="1.0">
            <entity>Contact</entity>
            <query>
                <field>phone<expression op="equals">#{phone}</expression></field>
            </query>
        </queryxml></ns0:sXML>
      </ns0:query>
   </ns1:Body>
</SOAP-ENV:Envelope>
'Content-Type': 'text/xml; charset=utf-8'