您的位置:首页 > 编程语言 > Python开发

Python与CMDBuild之API通过Token验证

2015-01-23 17:04 316 查看
安装配置不是什么难事相信同学们都很容易就完成了,这时最开始遇到的问题就是怎么通过CMDBuild的身份认证但是那两篇关于python对CMDBuild做二次开发的文章,,笔者去运行其提供的代码时与笔者自己写出的代码是报同样的错误,不知其笔者是否有未贴出来的代码或步骤。

好了,关于中文资料的问题,笔者的烦恼也吐槽完了(真的弄了好久,笔者新手啊)

CMDBuild版本:2.2.0

Python调用CMDBuild的soapAPI会出一系列的问题,一开始就是python的suds.client无法通过验证,提示为Head出错,经过研究之后,笔者在同事的帮助下通过SoapUI查看其java的Head(CMDBuild对java的支持比python好)然后自己把Head给拼出来。

   _Type = 'http://docs.oasis-open.org/wss/2004/01' \

           '/oasis-200401-wss-username-token-profile-1.0#PasswordText'

   _EncodingType = 'http://docs.oasis-open.org/wss/2004/01' \

                   '/oasis-200401-wss-soap-message-security-1.0#Base64Binary'

       然后加入进认证的步骤就可以了,代码如下:

       defxml(self):

       """

       Get xml representation of the object.

       @return: The root node.

       @rtype: L{Element}

       """

       root = Element('UsernameToken', ns=wssens)

       u = Element('Username', ns=wssens)

       u.setText(self.username)

       root.append(u)

       p = Element('Password', ns=wssens)

       p.set('Type', self._Type)

       p.setText(self.password)

       root.append(p)

 

       # set a new nonce every time invoked

       self.setnonce()

       if self.nonce is not None:

           n = Element('Nonce', ns=wssens)

           n.set('EncodingType', self._EncodingType)

           n.setText(self.nonce)

           root.append(n)

       if self.created is not None:

           n = Element('Created', ns=wsuns)

           n.setText(str(UTC(self.created)))

           root.append(n)

       return root

       现在我们可以通过认证了,但是问题又来了,我们无法解析CMDBuild的返回值。

???纳尼???

解决方法当然是有的,我们需要用suds中的suds.plugin,代码如下:

class EnsureSOAPBodyFilter(suds.plugin.MessagePlugin):

    def received(self, context):

        reply = context.reply

        m = P.search(reply)

        reply = m and m.group('envelope') orreply

        context.reply =  reply

然后我们就可以快乐的使用CMDbuild的SoapAPI了,完整代码如下:

# coding=utf8

 

import suds

import suds.wsse

importsuds.plugin

fromsuds.sax.element import Element

from suds.wsseimport wssens, wsuns

fromsuds.sax.date import UTC

import re

P =re.compile('(?P<envelope>(?=\<soap\:Envelope).+(?<=Envelope\>))')

classCMDUsernameToken(suds.wsse.UsernameToken):

    _Type ='http://docs.oasis-open.org/wss/2004/01' \

           '/oasis-200401-wss-username-token-profile-1.0#PasswordText'

    _EncodingType ='http://docs.oasis-open.org/wss/2004/01' \

                    '/oasis-200401-wss-soap-message-security-1.0#Base64Binary'

    def xml(self):

        """

        Get xml representation of the object.

        @return: The root node.

        @rtype: L{Element}

        """

        root = Element('UsernameToken', ns=wssens)

        u = Element('Username', ns=wssens)

        u.setText(self.username)

        root.append(u)

        p = Element('Password', ns=wssens)

        p.set('Type', self._Type)

        p.setText(self.password)

        root.append(p)

 

        # set a new nonce every time invoked

        self.setnonce()

        if self.nonce is not None:

            n = Element('Nonce', ns=wssens)

            n.set('EncodingType',self._EncodingType)

            n.setText(self.nonce)

            root.append(n)

        if self.created is not None:

            n = Element('Created', ns=wsuns)

            n.setText(str(UTC(self.created)))

            root.append(n)

        return root

 

classEnsureSOAPBodyFilter(suds.plugin.MessagePlugin):

    def received(self, context):

        reply = context.reply

        m = P.search(reply)

        reply = m and m.group('envelope') orreply

        context.reply =  reply

 

definit_client(url):

    wsse = suds.wsse.Security()

    token = CMDUsernameToken('admin', admin')

    token.setnonce()

    token.setcreated()

 

    wsse.tokens.append(token)

 

    c = suds.client.Client(url, wsse=wsse, plugins=[EnsureSOAPBodyFilter()])

 

    import logging

    logging.basicConfig()

   logging.getLogger('suds.client').setLevel(logging.DEBUG)

 

    return c

if __name__ =='__main__':

    WSDL = 'http://localhost:8080/cmdbuild/services/soap/Webservices?wsdl'

    c = init_client(WSDL)

 

    def createCard():

        cardType = c.factory.create('ns0:createCard').cardType

        cardType.beginDate='2014-12-02'

        cardType.className='Employee'

 

        createdId =c.service.createCard(cardType)

        return createdId

 

好了,关于python的CMDBuild的认证问题就这么解决了,这个方法可行但是有问题,勉强能用吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CMDBuild python api soap wsdl