Connecting to Microsoft Dynamics CRM 2011 SDK from Python
Question: Has anyone had any luck connecting to the 2011 endpoints in Microsoft Dynamics
CRM 2011 using Python? I've installed SUDS and can grab the WSDL, but service
calls return 400 Bad Request. I'm sure it's because I'm not authenticated, but
I'm not quite sure how to authenticate using raw SOAP. I know about [Girish's
code sample](http://code.msdn.microsoft.com/CRM-
Online-2011-WebServices-14913a16) but am not familiar enough with SOAP to go
much further.

I see this was asked [a year ago on
SO](http://stackoverflow.com/questions/3294587/how-can-i-connect-to-a-
microsoft-dynamics-crm-server-using-python) but that question seems to be
about NTLM. In my case, I want to use WS-Trust.

Here is the code I've got:

    
    
    from suds.client import Client
    import logging
    
    logging.basicConfig(level=logging.INFO)
    
    url = 'https://dev.crm.dynamics.com/XRMServices/2011/Discovery.svc?wsdl'
    client = Client(url)
    
    req = client.factory.create('ns3:RetrieveOrganizationsRequest')
    accessTypes = client.factory.create('ns3:AccessType')
    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)
    


Answer: If using SOAP isn't a requirement -- consider the [RESTish
Odata](http://msdn.microsoft.com/en-us/library/gg334279.aspx) service. I'm no
Python expert -- but you could run through GET requests in a browser and use
the debugger tools to see what the browser sends for headers and mimic this in
your Python code. You'd have some choice over what format the service returns
(JSON or XML) as well by modifying the Content-Type header.

