简体   繁体   中英

Connect to Dynamics CRM with python suds

I want to use microsoft CRM webservice, I tried this code:

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)

and I get this error:

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, )'

output for organization

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

error:

$ 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, )'

It looks like you are configured to use SOAP 1.2. Can you try your request with Soap 1.1?

I say this because I'm looking at this walk through: http://msdn.microsoft.com/en-us/library/gg594434.aspx

The request mentioned there conforms to the earlier standard (as indicated by the mime type.)

As far as how to do this with the suds library: After reading through the documentation a bit, I was inclined to rip my own eyes out. (Just kidding, I'm just used to reading a different type of documentation I suppose.) I'm sure you're far more used to browsing it than I am, and would be able to figure it out very quickly: http://jortel.fedorapeople.org/suds/doc/

finnally I connect to crm web service.but I get ""400, u'Bad Request'"" error when I execute my reques!

my code is:

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)

the output of print client:

$ 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

and the error I get :

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM