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版本: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的认证问题就这么解决了,这个方法可行但是有问题,勉强能用吧。
相关文章推荐
- 【总结】如何通过python调用新浪微博的API
- 微信公众平台 接口设置出现“你的服务器没有正确响应Token验证,请阅读消息接口使用指南”解决方案 Python
- 自己的服务器通过微信公众号Token验证测试的代码(Python版)
- python 数据加密以及生成token和token验证
- Python通过LDAP验证、查找用户(class,logging)
- 通过CouchDB-Python连接带有用户名/密码验证的数据库
- python使用json web token (jwt)实现http api的加密传输
- python学习系列之python装饰器基础(4)---装饰器实现token验证功能
- 自己的服务器通过微信公众号Token验证测试的代码(Python版)
- 黑龙江大学 校园网 师生服务 身份验证 查成绩 查绩点等 API 以及示例程序 (Python 验证码自动识别)
- Senium 2.0 通过webdriver提供的api完成各种验证操作
- Python与CMDBuild之CMDBuild基本配置-Class
- 通过redmine提供的api自动创建问题以方便与其他工具集成--------同事已验证过
- Robot自定义python关键字库通过ice中间件调用服务进行API测试
- Senium 2.0 通过webdriver提供的api完成各种验证操作
- python sina api token
- 如何通过python调用新浪微博的API
- Python3通过Luhn算法快速验证信用卡卡号的方法
- .NET WebAPI 用ActionFilterAttribute实现token令牌验证与对Action的权限控制
- python 产生token及token验证