nim*_*m4n 5 python suds dynamics-crm-2011
我想使用microsoft CRM webservice,我试过这段代码:
wsdl_url = 'http://crm-test:5555/CRMDeveleopment/XRMServices/2011/Organization.svc?wsdl'
username = 'user'
password = 'pass'
from suds.transport.https import WindowsHttpAuthenticated
from suds.client import Client
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)
ntlmTransport = WindowsHttpAuthenticated(username=username, password=password)
metadata_client = Client(wsdl_url, transport=ntlmTransport, cache=None)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Traceback (most recent call last):
File "crm.py", line 15, in <module>
metadata_client = Client(wsdl_url, transport=ntlmTransport)
File "/usr/lib/python2.7/site-packages/suds/client.py", line 112, in __init__
self.wsdl = reader.open(url)
File "/usr/lib/python2.7/site-packages/suds/reader.py", line 152, in open
d = self.fn(url, self.options)
File "/usr/lib/python2.7/site-packages/suds/wsdl.py", line 157, in __init__
self.open_imports()
File "/usr/lib/python2.7/site-packages/suds/wsdl.py", line 202, in open_imports
imp.load(self)
File "/usr/lib/python2.7/site-packages/suds/wsdl.py", line 314, in load
d = Definitions(url, options)
File "/usr/lib/python2.7/site-packages/suds/wsdl.py", line 159, in __init__
self.build_schema()
File "/usr/lib/python2.7/site-packages/suds/wsdl.py", line 220, in build_schema
self.schema = container.load(self.options)
File "/usr/lib/python2.7/site-packages/suds/xsd/schema.py", line 95, in load
child.dereference()
File "/usr/lib/python2.7/site-packages/suds/xsd/schema.py", line 323, in dereference
midx, deps = x.dependencies()
File "/usr/lib/python2.7/site-packages/suds/xsd/sxbasic.py", line 469, in dependencies
raise TypeNotFound(self.ref)
suds.TypeNotFound: Type not found: '(ManagedPropertyAttributeRequiredLevel, http://schemas.microsoft.com/xrm/2011/Contracts, )'
Run Code Online (Sandbox Code Playgroud)
组织输出
from suds import WebFault
from suds.client import *
from suds.transport.https import WindowsHttpAuthenticated
user = r'domain.net\username'
password = "password"
url = "http://crm-test:5555/XRMServices/2011/Organization.svc?wsdl"
ntlm = WindowsHttpAuthenticated(username = user, password = password)
client = Client(url, transport=ntlm)
print client
Run Code Online (Sandbox Code Playgroud)
错误:
$ python organization.py
DEBUG:suds.transport.http:opening (http://crm-test.kavatelecom.net:5555/XRMServices/2011/Organization.svc?wsdl)
DEBUG:suds.transport.http:opening (http://crm-test.kavatelecom.net:5555/XRMServices/2011/Organization.svc?wsdl=wsdl0)
Traceback (most recent call last):
File "organization.py", line 15, in <module>
client = Client(url, transport=ntlm)
File "/usr/lib/python2.7/site-packages/suds/client.py", line 112, in __init__
self.wsdl = reader.open(url)
File "/usr/lib/python2.7/site-packages/suds/reader.py", line 152, in open
d = self.fn(url, self.options)
File "/usr/lib/python2.7/site-packages/suds/wsdl.py", line 157, in __init__
self.open_imports()
File "/usr/lib/python2.7/site-packages/suds/wsdl.py", line 202, in open_imports
imp.load(self)
File "/usr/lib/python2.7/site-packages/suds/wsdl.py", line 314, in load
d = Definitions(url, options)
File "/usr/lib/python2.7/site-packages/suds/wsdl.py", line 159, in __init__
self.build_schema()
File "/usr/lib/python2.7/site-packages/suds/wsdl.py", line 220, in build_schema
self.schema = container.load(self.options)
File "/usr/lib/python2.7/site-packages/suds/xsd/schema.py", line 95, in load
child.dereference()
File "/usr/lib/python2.7/site-packages/suds/xsd/schema.py", line 323, in dereference
midx, deps = x.dependencies()
File "/usr/lib/python2.7/site-packages/suds/xsd/sxbasic.py", line 469, in dependencies
raise TypeNotFound(self.ref)
suds.TypeNotFound: Type not found: '(ManagedPropertyAttributeRequiredLevel, http://schemas.microsoft.com/xrm/2011/Contracts, )'
Run Code Online (Sandbox Code Playgroud)
最后我连接到 crm Web 服务。但是当我执行请求时,出现“400,u'Bad Request'”错误!
我的代码是:
from suds import WebFault
from suds.client import *
from suds.transport.https import WindowsHttpAuthenticated
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.transport.http').setLevel(logging.DEBUG)
user = r'domain\username'
password = "pass"
url = "http://domain:5555/XRMServices/2011/Discovery.svc?wsdl"
ntlm = WindowsHttpAuthenticated(username = user, password = password)
client = Client(url, transport=ntlm)
print client
req = client.factory.create('ns3:RetrieveOrganizationsRequest')
accessTypes = client.factory.create('ns3:EndpointAccessType')
orgRel = client.factory.create('ns3:OrganizationRelease')
req.AccessType = accessTypes.Default
req.Release = orgRel.Current
headers = dict()
headers['Content-Type'] = 'application/soap+xml'
client.set_options(headers=headers)
resp = client.service.Execute(req)
Run Code Online (Sandbox Code Playgroud)
打印客户端的输出:
$ python newcrm.py
Suds ( https://fedorahosted.org/suds/ ) version: 0.4 GA build: R699-20100913
Service ( DiscoveryService ) tns="http://schemas.microsoft.com/xrm/2011/Contracts"
Prefixes (4)
ns0 = "http://schemas.datacontract.org/2004/07/System.Collections.Generic"
ns1 = "http://schemas.microsoft.com/2003/10/Serialization/"
ns2 = "http://schemas.microsoft.com/xrm/2011/Contracts"
ns3 = "http://schemas.microsoft.com/xrm/2011/Contracts/Discovery"
Ports (1):
(CustomBinding_IDiscoveryService)
Methods (1):
Execute(ns3:DiscoveryRequest request, )
Types (24):
BaseServiceFault
ns3:DiscoveryRequest
ns3:DiscoveryResponse
DiscoveryServiceFault
ns3:EndpointAccessType
ns3:EndpointCollection
ns3:EndpointType
ErrorDetailCollection
ns0:KeyValuePairOfEndpointTypestringztYlk6OT
ns0:KeyValuePairOfstringanyType
ns3:OrganizationDetail
ns3:OrganizationDetailCollection
ns3:OrganizationRelease
OrganizationServiceFault
ns3:OrganizationState
ns3:RetrieveOrganizationRequest
ns3:RetrieveOrganizationResponse
ns3:RetrieveOrganizationsRequest
ns3:RetrieveOrganizationsResponse
ns3:RetrieveUserIdByExternalIdRequest
ns3:RetrieveUserIdByExternalIdResponse
ns1:char
ns1:duration
ns1:guid
Run Code Online (Sandbox Code Playgroud)
和我得到的错误:
DEBUG:suds.transport.http:sending:
URL:http://crm-test.kavatelecom.net:5555/XRMServices/2011/Discovery.svc
HEADERS: {'SOAPAction': u'"http://schemas.microsoft.com/xrm/2011/Contracts/Discovery/IDiscoveryService/Execute"', 'Content-Type': 'application/soap+xml', 'Content-type': 'application/soap+xml', 'Soapaction': u'"http://schemas.microsoft.com/xrm/2011/Contracts/Discovery/IDiscoveryService/Execute"'}
MESSAGE:
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.microsoft.com/xrm/2011/Contracts/Discovery" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><ns0:Body><ns1:Execute><ns1:request xsi:type="ns1:RetrieveOrganizationsRequest"><ns1:AccessType>Default</ns1:AccessType><ns1:Release>Current</ns1:Release></ns1:request></ns1:Execute></ns0:Body></SOAP-ENV:Envelope>
ERROR:suds.client:<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.microsoft.com/xrm/2011/Contracts/Discovery" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
<ns1:Execute>
<ns1:request xsi:type="ns1:RetrieveOrganizationsRequest">
<ns1:AccessType>Default</ns1:AccessType>
<ns1:Release>Current</ns1:Release>
</ns1:request>
</ns1:Execute>
</ns0:Body>
</SOAP-ENV:Envelope>
Traceback (most recent call last):
File "newcrm.py", line 32, in <module>
resp = client.service.Execute(req)
File "/usr/lib/python2.7/site-packages/suds/client.py", line 542, in __call__
return client.invoke(args, kwargs)
File "/usr/lib/python2.7/site-packages/suds/client.py", line 602, in invoke
result = self.send(soapenv)
File "/usr/lib/python2.7/site-packages/suds/client.py", line 649, in send
result = self.failed(binding, e)
File "/usr/lib/python2.7/site-packages/suds/client.py", line 708, in failed
raise Exception((status, reason))
Exception: (400, u'Bad Request')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1692 次 |
| 最近记录: |