JDBC Driver for Microsoft Dynamics 365

Build 23.0.8839

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 OAuth 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 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 OAuth Application 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: 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:

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

  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 create a custom OAuth application, see Creating a Custom OAuth 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:

  1. 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.
  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. 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 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 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.
  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 location of your OAuth settings file. Make sure this location 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 application 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 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 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 JDBC Driver 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 a Custom OAuth 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.

  1. 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.
  2. 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 a Custom OAuth 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.

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

  1. Open the Subscriptions page by searching and selecting the Subscriptions service from the search bar.
  2. Select the 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

To connect follow these steps:

  1. Set the AuthScheme to AzureServicePrincipal for using a client secret or AzureServicePrincipalCert for using a certificate in your application setting.
  2. Set the connection properties that apply to both schemes.
  3. 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.
Client Secret

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.

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 23.0.8839