JDBC Driver for Microsoft Dynamics 365

Build 22.0.8462

Establishing a Connection

Creating a JDBC Data Source

You can create a JDBC data source to connect from your Java application. Creating a JDBC data source based on the CData JDBC Driver for Microsoft Dynamics 365 consists of three basic steps:

  • Add the driver JAR file to the classpath. The JAR file is located in the lib subfolder of the installation directory. Note that the .lic file must be located in the same folder as the JAR file.
  • Provide the driver class. For example:
    cdata.jdbc.dynamics365.Dynamics365Driver
  • Provide the JDBC URL. For example:
    jdbc:dynamics365:InitiateOAuth=GETANDREFRESH;OrganizationUrl=https://myaccount.operations.dynamics.com/;Edition=Sales;
    
    or
    
    jdbc:cdata:dynamics365:InitiateOAuth=GETANDREFRESH;OrganizationUrl=https://myaccount.operations.dynamics.com/;Edition=Sales;

    The second format above can be used whenever there is a conflict in your application between drivers using the same URL format to ensure you are using the CData driver. The URL must start with either "jdbc:dynamics365:" or "jdbc:cdata:dynamics365:" and can include any of the connection properties in name-value pairs separated with semicolons.

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 a Custom AzureAD App 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: Set this to 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) Set this to the client Id in your application settings.
  • OAuthClientSecret: (custom applications only) Set this to the client secret in your application settings.
  • CallbackURL: Set this to the Redirect URL in your application settings.

When you connect the driver opens the OAuth endpoint in your default browser. Log in and grant permissions to the application. The driver then completes the OAuth process:

  1. Extracts the access token from the callback URL and authenticates requests.
  2. Obtains a new access token when the old one expires.
  3. 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 a Custom AzureAD App for more information about custom applications. You can then use the driver to get and manage the OAuth token values. Get an OAuth Access Token

Set the following connection properties to obtain the OAuthAccessToken:

  • AuthScheme: Set this to AzureAD.
  • OAuthClientId: Set this to the client Id in your application settings.
  • OAuthClientSecret: Set this to the client secret in your application settings

Then call stored procedures to complete the OAuth exchange:

  1. 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.

  2. Open the URL, log in, and authorize the application. You are redirected back to the callback URL.
  3. 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: Set this to REFRESH.
  • OAuthClientId: Set this to the client Id in your application settings.
  • OAuthClientSecret: Set this to the client secret in your application settings.
  • OAuthAccessToken: Set this to the access token returned by GetOAuthAccessToken.
  • OAuthRefreshToken: Set this to the refresh token returned by GetOAuthAccessToken.
  • OAuthSettingsLocation: Set this to the path where the driver saves the OAuth token values, which persist across connections.
On subsequent data connections, the values for OAuthAccessToken and OAuthRefreshToken are taken from OAuthSettingsLocation.

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: Set this to the client Id in your application settings.
  • OAuthClientSecret: Set this to 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.

  1. 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 driver 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.
  2. Then configure the driver 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 createa a custom OAuth application, see Creating a Custom AzureAD App. 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 connection property:

  1. Call the GetOAuthAuthorizationUrl stored procedure. Set the CallbackURL input to the Redirect URI you specified in your app settings.The stored procedure returns the URL to the OAuth endpoint and the PKCEVerifier.
  2. Open the returned URL in a browser. Log in and grant permissions to the driver. You are then redirected to the callback URL, which contains the verifier code.
  3. Save the value of the Verifier and the value of the PKCEVerifier. You need to set the value of the Verifier in the connection property and set the value of the PKCEVerifier in the 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 file.
  • 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 file containing the encrypted OAuth authentication values. Make sure this file 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 driver on another machine, authenticate, and then transfer the resulting OAuth values:

  1. On a second machine, install the driver and connect with the following properties set:
    • OAuthSettingsLocation: Set to a writable text file.
    • InitiateOAuth: Set to GETANDREFRESH.
    • OAuthClientId: Set to the client ID in your app settings.
    • OAuthClientSecret: Set to the client secret in your app settings.
    • CallbackURL: Set to the callback URL in your app settings.
  2. 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 path to your OAuth settings file. Make sure this file gives read and write permissions to the driver 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 app within the CData JDBC Driver 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 app in the Azure Portal, you must specify which permissions the app will require. Some permissions may be marked stating "Admin Consent Required". For example, all Groups permissions require Admin Consent. If your app 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 app you have created in App Registrations. Under API Permissions, there will be a button for Grant Consent. You can consent here for your app to have permissions on the tenant it was created under.

If your organization has multiple tenants or the app needs to be granted permissions for other tenants outside your organization, the GetAdminConsentURL may be used to generate the Admin Authorization url. Unlike the GetOAuthAuthorizationUrl, there will be no important information returned from this endpoint. If the grants access, it will simply return 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 app itself. All tasks taken by the app 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 app embedded with the CData JDBC Driver for Microsoft Dynamics 365 cannot be used in the client oauth flow. You must create your own OAuth app in order to use client credentials. See Creating a Custom AzureAD App for more details.

In your 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.

  1. Client Secret
    • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
    • AzureTenant: Set this to the tenant you wish to connect to.
    • OAuthGrantType: Set this to CLIENT.
    • OAuthClientId: Set this to the client Id in your app settings.
    • OAuthClientSecret: Set this to the client secret in your app settings.
  2. Certificate
    • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
    • AzureTenant: Set this to the tenant you wish to connect to.
    • OAuthGrantType: Set this to CLIENT.
    • OAuthClientId: Set this to the client Id in your app settings.
    • OAuthJWTCert: Set this to the JWT Certificate store.
    • OAuthJWTCertType: Set this to 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 a Custom AzureAD App.

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.

  1. Client Secret
    • AuthScheme: Set this to the "AzureServicePrincipal" in your app settings.
    • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the AzureAD exchange and manually setting the OAuthAccessToken.
    • AzureTenant: Set this to the tenant you wish to connect to.
    • OAuthClientId: Set this to the Client Id in your app settings.
    • OAuthClientSecret: Set this to the client secret in your app settings.
  2. Certificate
    • AuthScheme: Set this to the "AzureServicePrincipal" in your app settings.
    • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateAzureAD to avoid repeating the AzureAD exchange and manually setting the OAuthAccessToken.
    • AzureTenant: Set this to the tenant you wish to connect to.
    • OAuthClientId: Set this to the client Id in your app settings.
    • OAuthJWTCert: Set this to the JWT Certificate store.
    • OAuthJWTCertType: Set this to the type of the certificate store specified by OAuthJWTCert.

Azure Service Principal

Azure Service Principal is a connection type that goes through OAuth. Set your AuthScheme to AzureServicePrincipal.

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.

  1. Open the Subscriptions page by searching and selecting the Subscriptions service from the search bar.
  2. Select the particular subscription to assign the application to.
  3. Open the Access control (IAM) and select Add > Add role assignment to open the Add role assignment page.
  4. Select Owner as the role to assign to your created Azure AD app.
Complete the Authentication You are ready to connect after setting one of the below connection properties groups, depending on the configured app authentication (client secret or certificate).

Before choosing client secret or certicate authentication, follow these steps then continue to the relevant section below:

  1. AuthScheme: Set this to the AzureServicePrincipal in your app settings.
  2. InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
  3. AzureTenant: Set this to the tenant you wish to connect to.
  4. OAuthClientId: Set this to the client Id in your app settings.
Client Secret

Continue with the following:

  1. OAuthClientId: Set this to the client Id in your app settings.
  2. OAuthClientSecret: Set this to the client secret in your app settings.

Certificate

Continue with the following:

  1. OAuthJWTCert: Set this to the JWT Certificate store.
  2. OAuthJWTCertType: Set this to the type of the certificate store specified by OAuthJWTCert.

Follow the steps below to authenticate with the credentials for an AzureAD app. See Creating a Custom AzureAD Service Principal App.

AzureAD Service Principal App

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.

  1. Client Secret
    • AuthScheme: Set this to the "AzureServicePrincipal" in your app settings.
    • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the AzureAD exchange and manually setting the OAuthAccessToken.
    • AzureTenant: Set this to the tenant you wish to connect to.
    • OAuthClientId: Set this to the Client Id in your app settings.
    • OAuthClientSecret: Set this to the Client Secret in your app settings.
  2. Certificate
    • AuthScheme: Set this to the "AzureServicePrincipal" in your app settings.
    • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateAzureAD to avoid repeating the AzureAD exchange and manually setting the OAuthAccessToken.
    • AzureTenant: Set this to the tenant you wish to connect to.
    • OAuthClientId: Set this to the Client Id in your app settings.
    • OAuthJWTCert: Set this to the JWT Certificate store.
    • OAuthJWTCertType: Set this to the type of the certificate store specified by OAuthJWTCert.

MSI

If you are running Microsoft Dynamics 365 on an Azure VM, you can leverage Managed Service Identity (MSI) credentials to connect:
  • AuthScheme: Set this to AzureMSI.

The MSI credentials are automatically obtained for authentication.

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8462