Package: dynamics365crm-python
dynamics365crm-pythonDynamics365CRM API wrapper for Dynamics 365 written in Python.
This library works for API version: v9.0Installingpip install dynamics365crm-pythonUsageThis library provides a client that is initialized with the following argumentsdomain: the dynamics 365 tenant domain (yours or someone else's)access_token: the retrieved token after authenticationArguments for OAuth2 flowclient_id: your Azure AD application client idclient_secret: your Azure AD application client secretfromdynamics365crm.clientimportClient## Normal use to make calls to the apiclient=Client("https://tenant_name.crmX.dynamics.com",access_token="access_token")## OAuth2 configuration required argumentsclient=Client("https://tenant_name.crmX.dynamics.com",client_id=CLIENT_ID,client_secret=CLIENT_SECRET,)OAuth2 ProtocolGet authorization urlThis will return aMSALvalid authorization url, the following are required:tenant_id: someone else's Azure AD tenant_idAsk the dynamics tenant owner to go to theAzure Portaland retrieve the Tenant ID from the Azure Active Directory/OverviewIf your app is configured as multi-tenant (for any enterprise or personal account to use) you could pass "common" instead od the Tenant IDHowever microsoft azure app configuration is a mess so the Tenant ID is preferableredirect_uri: your service callback urlstate: your unique generated state to identify the requesteryou could also initiate an oauth flow with msal manually with initiate_auth_code_flow method, check theofficial exampleauthorization_url=client.build_authorization_url("tenant_id","redirect_uri","state")>>>"https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=XXXX&response_type=code&redirect_uri=https%3A%2F%your_domain/%2Fcallback%2F&scope=https%3A%2F%2tenant_name.crmX.dynamics.com%2Fuser_impersonation+offline_access+openid+profile&state=XXXX&prompt=consent"Exchange the callback code for an access tokenTo finish the oauth protocol microsoft will redirect to your callback endpoint with a temporal code in the url query params to be exchanged for the full-fledged token (a json with the access_token, refresh_token, expires_in, etc.)Again the (tenant_idor "common") andredirect_uriare required, the third argument is the code sent by microsofttoken=client.exchange_code("tenant_id","redirect_uri","code")Refresh tokenIf the access token expires you could get a newaccess_tokenexchanging the long-livedrefresh_tokenAgain thetenant_idor "common" is requiredtoken=client.refresh_access_token("tenant_id","refresh_token")Set access tokenYou could pass the access_token in the constructor or set it withclient.set_access_token("access_token")Dynamics Web APIContactsSee the documentationhttps://docs.microsoft.com/es-es/dynamics365/customer-engagement/web-api/contact?view=dynamics-ce-odata-9Get Contactscan receive orderby, filter, select, top, expandlist_contacts = client.get_contacts()Create Contactcreate_contact = client.create_contact(firstname="FIRSTNAME", lastname="LASTNAME", middlename="MIDDLENAME", emailaddress1="EMAILADDRESS")Delete Contactdelete_contact = client.delete_contact('ID')Update Contactupdate_contact = client.update_contact('ID', firstname="FIRSTNAME", lastname="LASTNAME", middlename="MIDDLENAME", emailaddress1="EMAILADDRESS")AccountsSee the documentationhttps://docs.microsoft.com/es-es/dynamics365/customer-engagement/web-api/account?view=dynamics-ce-odata-9Get Accountscan receive orderby, filter, select, top, expandget_accounts = client.get_accounts()Create Accountcreate_account = client.create_account(name="NAME", websiteurl="WWW.WEBSITE.COM")Delete Accountcreate_account = client.delete_account('ID')Update Accountupdate_account = client.update_account(id="ID", name="NAME")OpportunitiesSee the documentationhttps://docs.microsoft.com/es-es/dynamics365/customer-engagement/web-api/opportunity?view=dynamics-ce-odata-9Get Opportunitiescan receive orderby, filter, select, top, expandlist_opportunities = client.get_opportunities()Create Opportunitiescreate_opportunities = client.create_opportunity(name="OPPORTUNITY NAME")Delete Opportunitiesdelete_opportunities = client.delete_opportunity(id="OPPORTUNITY ID")Update Opportunitiesupdate_opportunities = client.update_opportunity(id="OPPORTUNITY ID", name="OPPORTUNITY NAME", description="SOME DESCRIPTION")LeadsSee the documentationhttps://docs.microsoft.com/es-es/dynamics365/customer-engagement/web-api/lead?view=dynamics-ce-odata-9Get Leadscan receive orderby, filter, select, top, expandlist_leads = client.get_leads()Create Leadcreate_leads = client.create_lead(fullname="LEAD NAME", subject="LEAD SUBJECT", mobilephone="123456", websiteurl="WWW.WEBSITE.COM", middlename="MIDDLE LEAD NAME")Delete Leaddelete_leads = client.delete_lead("ID")Update Leadupdate_leads = client.update_lead(fullname="LEAD NAME", subject="LEAD SUBJECT", mobilephone="123456", websiteurl="WWW.WEBSITE.COM", middlename="MIDDLE LEAD NAME")CampaignsSee the documentationhttps://docs.microsoft.com/es-es/dynamics365/customer-engagement/web-api/campaign?view=dynamics-ce-odata-9Get Campaignscan receive orderby, filter, select, top, expandlist_campaigns = client.get_campaigns()Create Campaigncreate_campaign = client.create_campaign(name="CAMPAIGN NAME", description="SOME DESCRIPTION")Delete Campaigndelete_campaign = client.delete_campaign(id="ID")Update Campaignupdate_campaign = client.update_campaign(id="ID", name="CAMPAIGN NAME", description="SOME DESCRIPTION")RequirementsrequestsmsalContributingWe are always grateful for any kind of contribution including but not limited to bug reports, code enhancements, bug fixes, and even functionality suggestions.You can report any bug you find or suggest new functionality with a newissue.If you want to add yourself some functionality to the wrapper:Fork it (https://github.com/GearPlug/dynamics365crm-python)Create your feature branch (git checkout -b my-new-feature)Commit your changes (git commit -am 'Adds my new feature')Push to the branch (git push origin my-new-feature)Create a new Pull Request