Python SOAP客户端,授权

时间:2014-05-14 06:14:38

标签: php python web-services soap wsdl

我正在尝试授权外部API,它基于SOAP和WSDL。 我在这项技术上没有太多经验,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请求(使用任何lib,可能是SUDS或SOAPpy)?知道如何授权?

1 个答案:

答案 0 :(得分: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>
相关问题