How it works...

This abridged Java code is a simple example of how you can use non-.NET code to connect to your Dynamics 365 instance. For the sake of simplicity, let's say the code is far from following Java code best practices. For instance, you would expect connection values to be configurable rather than hardcoded. You would also expect the application to be divided into layers.

In step 1, we started by creating a Java class called JavaDynamics365 with the package name com.packt.dynamics365 (make sure you store your Java file in the correct folder compacktdynamics365 to abide by the convention). The package name follows the Oracle-recommended reversed Internet domain convention: https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html.

In step 2, we referenced the libraries that we will be using in our code. We've got java.net libraries to manipulate our HTTP request, org.json libraries to deal with JSON parsing, com.microsoft.aad.adal4j for the Azure AD authentication, and finally, some java.util libraries to help with the authentication method.

In step 3, we defined some static strings, such as our connection details, the client ID, and the login URL with the tenant GUID, as defined in the Getting ready section. As explained previously, it's best practice to extract these into a secure configurable container.

In step 4, we got the authentication token from Azure AD by calling the acquireToken method. This method from the ADAL library encapsulates the mechanism required to log in to Azure AD and get the required token. The getAccessTokenFromUserCredentials method is reused from the Microsoft Azure Samples GitHub repository: https://github.com/Azure-Samples/active-directory-java-native-headless.

In step 5, we issued an HTTP GET request with an Authorization header to contain the retrieved token. We then parsed the result into JSON arrays and looped through each returned object to display the name of the retrieved organizations. This method is similar to the Web API request discussed in Querying Dynamics 365 Data using the Web API endpoint recipe of Chapter 2, Client-Side Extensions.

In step 6, we created a main method to call the token retrieval method and call the getAccounts method with the retrieved token.

Finally, in step 7 and step 8, we compiled and ran the console application. Note how we referred to a wild card * for referenced dependencies. The list of dependencies is as follows:

  • activation-1.1.jar
  • adal4j-1.1.1.jar
  • bcprov-jdk15on-1.51.jar
  • commons-codec-1.10.jar
  • commons-lang3-3.3.1.jar
  • gson-2.2.4.jar
  • jcip-annotations-1.0.jar
  • json-smart-1.1.1.jar
  • lang-tag-1.4.jar
  • mail-1.4.7.jar
  • nimbus-jose-jwt-3.1.2.jar
  • oauth2-oidc-sdk-4.5.jar
  • org.json.jar
  • slf4j-api-1.7.5.jar

However, the main three libraries are adal4j, oauth2-oidc-sdk, and json.

If you have Maven installed, you can retrieve all the necessary libraries and their dependencies using the copy dependencies command, as follows:

mvn dependency:copy-dependencies

To do so, you will need a pom.xml file with the following dependencies:

<dependencies> 
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>adal4j</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>oauth2-oidc-sdk</artifactId>
<version>4.5</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
</dependencies>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.180.76