Establishing a Connection
The objects available within our connector are accessible from the "cdata.dynamics365" module. To use the module's objects directly:
- Import the module as follows:
import cdata.dynamics365 as mod
- To establish a connection string, call the connect() method from the connector object using an appropriate connection string, such as:
mod.connect("InitiateOAuth=GETANDREFRESH;OrganizationUrl=https://myaccount.operations.dynamics.com/;Edition=Sales;")
Connecting to Microsoft Dynamics 365
Edition and OrganizationUrl are required connection properties for connection. The Microsoft Dynamics 365 driver supports connecting to the following Microsoft Dynamics 365 editions:- CustomerService
- FieldService
- FinOpsOnline
- FinOpsOnPremise
- HumanResources
- Marketing
- ProjectOperations
- Sales
Note that "Supply Chain Management" is the same as "Finance and Operations." You can connect to it by setting the Edition to either "FinOpsOnline" or "FinOpsOnPremise."
For Microsoft Dynamics 365 Business Central, use the separate Microsoft Dynamics 365 Business Central driver.
OrganizationUrl is the URL to your Microsoft Dynamics 365 organization. For instance, https://orgcb42e1d0.crm.dynamics.com
Authenticating to Microsoft Dynamics 365
OAuth
If required by your organization, Admin Consent is needed when authorizing a new OAuth application for your Azure Tenant. In all OAuth flows, the first time someone in your organization installs and uses an OAuth application, an administrator for the organization must approve the application for their Azure Tenant.Your organization can choose, however, to disable this requirement.
Desktop Applications
CData provides an embedded OAuth application that simplifies OAuth desktop Authentication. Alternatively, you can create a custom OAuth application. See Creating an Azure AD Application for information about creating custom applications and reasons for doing so.
For authentication, the only difference between the two methods is that you must set two additional connection properties when using custom OAuth applications. After setting the following connection properties, you are ready to connect:
- AuthScheme: The AzureAD in your application settings.
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- OAuthClientId: (custom applications only) The client Id in your application settings.
- OAuthClientSecret: (custom applications only) The client secret in your application settings.
- CallbackURL: The Redirect URL in your application settings.
When you connect the connector opens the OAuth endpoint in your default browser. Log in and grant permissions to the application. The connector then completes the OAuth process:
- Extracts the access token from the callback URL and authenticates requests.
- Obtains a new access token when the old one expires.
- Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
Web Applications
When connecting via a Web application, you need to create and register a custom OAuth application with Microsoft Dynamics 365. See Creating an Azure AD Application for more information about custom applications. You can then use the connector to get and manage the OAuth token values.
Get an OAuth Access Token
Set the following connection properties to obtain the OAuthAccessToken:
- AuthScheme: AzureAD.
- OAuthClientId: The client Id in your application settings.
- OAuthClientSecret: The client secret in your application settings
Then call stored procedures to complete the OAuth exchange:
- Call the GetOAuthAuthorizationUrl stored procedure. Set the CallbackURL input to the callback URL you specified in your application settings. If necessary, set the Scope parameter to request custom permissions. The stored procedure returns the URL to the OAuth endpoint.
- Open the URL, log in, and authorize the application. You are redirected back to the callback URL.
- Call the GetOAuthAccessToken stored procedure. Set the AuthMode input to WEB. Set the Verifier input to the "code" parameter in the query string of the callback URL. If necessary, set the Scope parameter to request custom permissions.
Once you have obtained the access and refresh tokens, you can connect to data and refresh the OAuth access token either automatically or manually.
Automatic Refresh of the OAuth Access Token
To have the driver automatically refresh the OAuth access token, set the following on the first data connection:
- InitiateOAuth: REFRESH.
- OAuthClientId: The client Id in your application settings.
- OAuthClientSecret: The client secret in your application settings.
- OAuthAccessToken: The access token returned by GetOAuthAccessToken.
- OAuthRefreshToken: The refresh token returned by GetOAuthAccessToken.
- OAuthSettingsLocation: The location where the connector saves the OAuth token values, which persist across connections.
Manual Refresh of the OAuth Access Token
The only value needed to manually refresh the OAuth access token when connecting to data is the OAuth refresh token.
Use the RefreshOAuthAccessToken stored procedure to manually refresh the OAuthAccessToken after the ExpiresIn parameter value returned by GetOAuthAccessToken has elapsed, then set the following connection properties:
- OAuthClientId: The client Id in your application settings.
- OAuthClientSecret: The client secret in your application settings.
Then call RefreshOAuthAccessToken with OAuthRefreshToken set to the OAuth refresh token returned by GetOAuthAccessToken. After the new tokens have been retrieved, open a new connection by setting the OAuthAccessToken property to the value returned by RefreshOAuthAccessToken.
Finally, store the OAuth refresh token so that you can use it to manually refresh the OAuth access token after it has expired.
Headless Machines
To configure the driver to use OAuth with a user account on a headless machine, you need to authenticate on another device that has an internet browser.
- Choose one of these two options:
- Option 1: Obtain the OAuthVerifier value as described in "Obtain and Exchange a Verifier Code" below.
- Option 2: Install the connector on another machine and transfer the OAuth authentication values after you authenticate through the usual browser-based flow, as described in "Transfer OAuth Settings" below.
- Then configure the connector to automatically refresh the access token from the headless machine.
You can follow the headless OAuth authentication flow using the OAuth credentials for your custom OAuth app. To create a custom OAuth application, see Creating an Azure AD Application. This section describes the procedure to authenticate and connect to data.
Option 1: Obtain and Exchange a Verifier Code
First, set the following properties on the headless machine:
- InitiateOAuth: Set to OFF.
- OAuthClientId: Set to the Client ID in your OAuth Integration settings.
- OAuthClientSecret: Set to the Client Secret in your OAuth Integration settings.
Next, authenticate from another machine and obtain the OAuthVerifier connection property:
- Call the GetOAuthAuthorizationUrl stored procedure. Set the CallbackURL input to the Redirect URI you specified in your application settings. The stored procedure returns the URL to the OAuth endpoint.
- Open the returned URL in a browser. Log in and grant permissions to the connector. You are then redirected to the callback URL, which contains the verifier code.
- Save the value of the Verifier. You must set the value of the Verifier in the OAuthVerifier connection property.
Finally, on the headless machine, set the following connection properties to obtain the OAuth authentication values:
- OAuthClientId: Set to the Client ID in your OAuth Integration settings.
- OAuthClientSecret: Set to the Client Secret in your OAuth Integration settings.
- OAuthVerifier: Set to the verifier code.
- OAuthSettingsLocation: Set to persist the encrypted OAuth authentication values to the specified location.
- InitiateOAuth: Set to REFRESH.
Connect to Data
After the OAuth settings file is generated, set the following properties to connect to data:
- OAuthSettingsLocation: Set to the location containing the encrypted OAuth authentication values. Make sure this location gives read and write permissions to the provider to enable the automatic refreshing of the access token.
- InitiateOAuth: Set to REFRESH.
Option 2: Transfer OAuth Settings
To install the connector on another machine, authenticate, and then transfer the resulting OAuth values:
- On a second machine, install the connector and connect with the following properties set:
- OAuthSettingsLocation: Set to a writable location.
- InitiateOAuth: Set to GETANDREFRESH.
- OAuthClientId: Set to the client ID in your application settings.
- OAuthClientSecret: Set to the client secret in your application settings.
- CallbackURL: Set to the callback URL in your application settings.
- Test the connection to authenticate. The resulting authentication values are written and encrypted to the path specified by OAuthSettingsLocation. After you have successfully tested the connection, copy the OAuth settings file to your headless machine. On the headless machine, set the following connection properties to connect to data:
- InitiateOAuth: Set to REFRESH.
- OAuthSettingsLocation: Set to the location of your OAuth settings file. Make sure this location gives read and write permissions to the connector to enable the automatic refreshing of the access token.
Admin Consent
Admin consent refers to when the Admin for an Azure Active Directory tenant grants permissions to an application which requires an admin to consent to the use case. The embedded application within the CData Python Connector for Microsoft Dynamics 365, contains no permissions that require admin consent. Therefore, this information applies only to custom applications.Admin Consent Permissions
When creating a new OAuth application in the Azure Portal, you must specify which permissions the application requires. Some permissions may be marked stating "Admin Consent Required". For example, all Groups permissions require Admin Consent. If your application requires admin consent, there are a couple of ways this can be done.
The easiest way to grant admin consent is to just have an admin log into portal.azure.com and navigate to the application you have created in App Registrations. Under API Permissions, click Grant Consent, which grants your application the necessary permissions on the tenant on which it was created.
If your organization has multiple tenants or the application needs to be granted permissions for other tenants outside your organization, you can use the GetAdminConsentURL to generate the Admin Authorization URL. Unlike the GetOAuthAuthorizationUrl, there is no important information returned from this endpoint. If the tenant grants access, it returns a boolean indicating that permissions were granted.
After an admin grants consent, authentication may be performed as normal.
Client Credentials
Client credentials refers to a flow in OAuth where there is no direct user authentication taking place. Instead, credentials are created for just the application itself. All tasks taken by the application are done without a default user context. This makes the authentication flow a bit different from standard.Client OAuth Flow
All permissions related to the client oauth flow require admin consent. This means the application embedded with the CData Python Connector for Microsoft Dynamics 365 cannot be used in the client oauth flow. You must create your own OAuth application to use client credentials. See Creating an Azure AD Application for more details.
In "App Registration" in portal.azure.com, navigate to API Permissions and select the Microsoft Graph permissions. There are two distinct sets of permissions - Delegated and Application permissions. The permissions used during client credential authentication are under Application Permissions. Select the applicable permissions you require for your integration.
You are ready to connect after setting one of the below connection properties groups depending on the authentication type.
- Client Secret
- InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- AzureTenant: The tenant you wish to connect to.
- OAuthGrantType: CLIENT.
- OAuthClientId: The client Id in your app settings.
- OAuthClientSecret: The client secret in your app settings.
- Certificate
- InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- AzureTenant: The tenant you wish to connect to.
- OAuthGrantType: CLIENT.
- OAuthClientId: The client Id in your app settings.
- OAuthJWTCert: The JWT Certificate store.
- OAuthJWTCertType: The type of the certificate store specified by OAuthJWTCert.
Azure AD
Azure AD is a connection type that leverages OAuth to authenticate. Set your AuthScheme to AzureAD.Follow the steps below to authenticate with the credentials for a custom AzureAD app. See Creating an Azure AD Application.
There are two types of app authentication available: using a client secret and using a certificate. You can use any of them depending on the configured app authentication.
Get an AzureAD Access Token
You are ready to connect after setting one of the below connection properties groups depending on the authentication type.
- Client Secret
- AuthScheme: AzureServicePrincipal in your app settings.
- InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the AzureAD exchange and manually setting the OAuthAccessToken.
- AzureTenant: The tenant you wish to connect to.
- OAuthClientId: The Client Id in your app settings.
- OAuthClientSecret: The client secret in your app settings.
- Certificate
- AuthScheme: The AzureServicePrincipal in your app settings.
- InitiateOAuth: GETANDREFRESH. You can use InitiateAzureAD to avoid repeating the AzureAD exchange and manually setting the OAuthAccessToken.
- AzureTenant: The tenant you wish to connect to.
- OAuthClientId: The client Id in your app settings.
- OAuthJWTCert: The JWT Certificate store.
- OAuthJWTCertType: The type of the certificate store specified by OAuthJWTCert.
Azure Service Principal
The authentication as an Azure Service Principal is handled via the OAuth Client Credentials flow. It does not involve direct user authentication. Instead, credentials are created for just the application itself. All tasks taken by the app are done without a default user context, but based on the assigned roles. The application access to the resources is controlled through the assigned roles' permissions. Create an AzureAD App and an Azure Service Principal When authenticating using an Azure Service Principal, you must register an application with an Azure AD tenant. See Creating a Custom AzureAD Service Principal App for more information.
Assign a role to the application
To access resources in your subscription, you must assign a role to the application.
- Open the Subscriptions page by searching and selecting the Subscriptions service from the search bar.
- Select the subscription to assign the application to.
- Open the Access control (IAM) and select Add > Add role assignment to open the Add role assignment page.
- Select Owner as the role to assign to your created Azure AD app.
To connect follow these steps:
- Set the AuthScheme to AzureServicePrincipal for using a client secret or AzureServicePrincipalCert for using a certificate in your application setting.
- Set the connection properties that apply to both schemes.
- Set the connection properties specific to the authentication scheme you selected.
For Both Client Secret and Certificate
Set these connection properties and then continue to the relevant section below:
- InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- AzureTenant: The tenant you want to connect to.
Continue with the following:
- OAuthClientId: The client Id in your application settings.
- OAuthClientSecret: The client secret in your application settings.
Certificate
Continue with the following:
- OAuthJWTCert: The JWT Certificate store.
- OAuthJWTCertType: The type of the certificate store specified by OAuthJWTCert.