2014-05-14 372 views
1

我想授權給基於SOAP和WSDL的外部API。 我對這項技術沒有多少經驗,API文檔基於PHP。 我無法在Python中授權(我嘗試使用SUDS)。這裏是PHP的(工作)代碼:Python SOAP客戶端,授權

class HRDConfig { 
    public $uid = "__partner_uid__"; 
    public $pass = "__encoded_cspNr_and_pass__"; // ie. "csp123pass" 

    const NS = "https://www.hrd.pl/partnerAPI/"; 
    const PARTNER = "https://www.hrd.pl/partnerAPI/Partner.php?wsdl"; 
    const DOMAIN = "https://www.hrd.pl/partnerAPI/Domain.php?wsdl"; 
    const CERTIFICATE = "https://www.hrd.pl/partnerAPI/Certificate.php?wsdl"; 
    const CLIENT = "https://www.hrd.pl/partnerAPI/Client.php?wsdl"; 
    const POLL = "https://www.hrd.pl/partnerAPI/Poll.php?wsdl"; 
    const INVOICE = "https://www.hrd.pl/partnerAPI/Invoice.php?wsdl"; 
} 

ini_set("soap.wsdl_cache_enabled", "1"); //enable cache 
$soap = new SoapClient(HRDConfig::DOMAIN, array("encoding"=>"UTF-8", "exceptions" => true)); 
$soap->__setSoapHeaders(array(new SoapHeader(HRDConfig::NS, "AuthHeader", new HRDConfig()))); 

我想將此代碼移動到python,但沒有任何成功。我如何將AuthHeader添加到SOAP請求中(使用任何lib,可能是SUDS或SOAPpy)?任何想法如何授權?

回答

1

即使由於缺乏該服務的憑證而無法驗證它是否正常工作,也應該從suds開始。 只需創建一個HRDPartnerAPI實例,調用add_authheader.service.<methodname>(<args...>)

import suds.client 
from suds.sax.element import Element 
from suds.sax.attribute import Attribute 

class HRDPartnerAPI(object): 

    NS = "https://www.hrd.pl/partnerAPI/" 
    PARTNER = "http://www.hrd.pl/partnerAPI/Partner.php?wsdl" 
    DOMAIN = "https://www.hrd.pl/partnerAPI/Domain.php?wsdl" 
    CERTIFICATE = "https://www.hrd.pl/partnerAPI/Certificate.php?wsdl" 
    CLIENT = "https://www.hrd.pl/partnerAPI/Client.php?wsdl" 
    POLL = "https://www.hrd.pl/partnerAPI/Poll.php?wsdl" 
    INVOICE = "https://www.hrd.pl/partnerAPI/Invoice.php?wsdl" 

    def __init__(self, wsdl): 
     self.client = suds.client.Client(wsdl) 
     self.service = self.client.service 

    def add_authheader(self, username, password): 
     auth = Element('AuthHeader'); 
     login = Element('uid').setText(username) 
     secret = Element('pass').setText(password) 
     auth.append(login); 
     auth.append(secret); 
     self.client.set_options(soapheaders=[auth]) 

    def list_methods(self): 
     return [method for method in self.client.wsdl.services[0].ports[0].methods] 

domain = HRDPartnerAPI(wsdl=HRDPartnerAPI.DOMAIN) 
print domain.list_methods() # unauth 
print domain.service.check("lala.com") # unauth 
print domain.service.validateAuthInfo("hrd.pl","abcd") # unauth 
print domain.add_authheader("__partner_uid__", "__encoded_cspNr_and_pass__") # add auth info 
print domain.service.check("lala.com") # authenticated 
print domain.client.last_sent() # DEBUG: show last sent msg, shows AuthHeader is present. 
print domain.client.last_received() # DEBUG: show last recv. msg 

輸出添加AuthHeader,並調用任何方法:

[info, domainInSystem, check, changeDns, changePrivacy, createHost, transfer, register, trade, validateAuthInfo, renew, validateTradePw, transferDelete, listByClient, tradeStatus, privacyAddDomain, listAll, privacyRemoveDomain] 
{"status":false,"errorCode":"1032","error":"Auth Error"} 
{"status":false,"errorCode":"1032","error":"Auth Error"} 
None 
{"status":false,"errorCode":"1032","error":"Auth Error"} 

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:ns3="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="https://www.hrd.pl/partnerAPI/" xmlns:ns2="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:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <SOAP-ENV:Header> 
     <AuthHeader> 
     <uid>__partner_uid__</uid> 
     <pass>__encoded_cspNr_and_pass__</pass> 
     </AuthHeader> 
    </SOAP-ENV:Header> 
    <ns2:Body> 
     <ns1:check> 
     <domains xsi:type="ns3:string">lala.com</domains> 
     </ns1:check> 
    </ns2:Body> 
</SOAP-ENV:Envelope> 

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <SOAP-ENV:Body> 
     <ns1:checkResponse> 
     <return xsi:type="xsd:string">{&quot;status&quot;:false,&quot;errorCode&quot;:&quot;1032&quot;,&quot;error&quot;:&quot;Auth Error&quot;}</return> 
     </ns1:checkResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope>