【问题标题】:Python SOAP client, authorizationPython SOAP 客户端,授权
【发布时间】:2014-05-14 06:14:38
【问题描述】:

我正在尝试对基于 SOAP 和 WSDL 的外部 API 进行授权。 我对这项技术没有太多经验,API 文档是基于 PHP 的。 我无法在 Python 中授权(我尝试使用 SUDS)。这是 PHP 的(工作)代码:

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 请求(使用任何库,可能是 SUDS 或 SOAPpy)?知道如何授权吗?

【问题讨论】:

    标签: php python web-services soap wsdl


    【解决方案1】:

    这应该让您开始使用suds,即使由于缺少该服务的凭据,我无法验证它是否实际工作。 只需创建一个HRDPartnerAPI 实例,调用add_authheader 来添加您的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
    

    输出:

    [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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 2020-07-14
      相关资源
      最近更新 更多