2015-05-08 52 views
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> 

回答

0

我有SUDS庫將AutoTask SOAP API集成到我的項目中。

我覺得你想念肥皂信封標題標籤。由於我直接調用API函數,所以使用SUDS庫形成信封。請嘗試糾正你的信封如下:

<?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' 

請隨時聯繫我,如果你想使用儀器的樣品。