Quantcast
Channel: Microsoft Dynamics 365 Community
Viewing all articles
Browse latest Browse all 13977

CRM Web API Using Java

$
0
0
Here’s an example that consumes the new CRM Web API from a Java application. I’m not promising this is the best written Java but it appears to get the job done.

Again our friends at Microsoft help us out on the authentication front by providing a version of the Azure Active Directory Authentication Library (ADAL) for Java. You can set up a Maven dependency with the info here: http://mvnrepository.com/artifact/com.microsoft.azure/adal4j

In this case I’m authentication using a hardcoded username and password.

//Azure Application Client ID
private final static String CLIENT_ID = "00000000-0000-0000-0000-000000000000";
//CRM URL
private final static String RESOURCE = "https://org.crm.dynamics.com";
//O365 credentials for authentication w/o login prompt
private final static String USERNAME = "administrator@org.onmicrosoft.com";
private final static String PASSWORD = "password";
//Azure Directory OAUTH 2.0 AUTHORIZATION ENDPOINT
private final static String AUTHORITY =
"https://login.microsoftonline.com/00000000-0000-0000-0000-000000000000";

AuthenticationContext context = null;
AuthenticationResult result = null;
ExecutorService service = null;
try {
service = Executors.newFixedThreadPool(1);
context = new AuthenticationContext(AUTHORITY, false, service);
Future<AuthenticationResult> future = context.acquireToken(RESOURCE,
CLIENT_ID,
USERNAME,
PASSWORD, null);
result = future.get();
} finally {
service.shutdown();
}

String token = result.getAccessToken();

The other thing I stumbled upon is that Java’s HttpURLConnection for making HTTP requests doesn’t support the PATCH method natively (which is used by the Web API when doing updates to multiple fields). This was solved specifying a POST method and adding an additional “X-HTTP-Method-Override” property.

connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
connection.setRequestMethod("POST");

You can check out the code on GitHub:

https://github.com/jlattimer/CrmWebApiJava

 
Web API Examples:



Viewing all articles
Browse latest Browse all 13977

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>