How to Connect your Java Application to CRM: A Simple Java Console Application
Have you ever tried to connect to CRM Online using a Java Application? Turns out it’s much simpler than you would expect. By leveraging the Azure Active Directory Library, you can easily authenticate any user for your external application to perform any CRM Operation through the Web API.
Here are the required components:
- CRM Online (Trial will still work)
- Azure Active Directory
- OAUTH 2.0 Authorization Endpoint
- Application ID
- A Java IDE (Maven project is recommended for the dependencies)
Here’s a quick summary of how this actually works:
- Obtain the access token, by using the OAUTH 2.0 Authorization Endpoint.
- Build the connection with your Dynamics CRM URI, and set the request method and properties with the access token.
- Finally, depending on the Request type, you build the JSON Object and write it to CRM or save it to view.
Let’s dig deeper and get into the details:
First, you need the access token. To obtain it, you will need four parts: the Dynamics CRM Uri, the username and password of the account, and the application id that you created in your Azure Active Directory when you registered the application. Then, using ADAL’s AuthenticationContext with the Authorization Endpoint URL, you will obtain your access token.
For this specific example, we will be performing a simple WhoAmI function. To start, you pass in the access token, and create your URL from your CRM URI and then build the connection (we will go into detail about how that works later). If successful, you will build your JSON Object (we will also demonstrate how to do this later), grab the user id, and return it.
Here, we will build the connection by setting the request headers and adding the access token, which allows CRM to authenticate your user.
This method is used to get the JSON Object from our request. We store everything read from the request into a StringBuffer and then parse that into a JSON Object.
Now, to fire everything off, in your main method you will be prompt your user to enter their credentials. With these credentials, you will get the access token. Finally, you call the WhoAmI method and get the current User’s Id. With that, you have a simple connection to CRM with JAVA using ADAL.
Connecting to CRM using Java has never been easier! If you were worried about using CRM, but afraid of the limits of being able to connect it to your Java platform, just fill out the contact sheet below. We will be more than happy to answer your questions.