1

I have a requirement to create restful service using WebApi in MS Dynamics and create a client in Java and hit the MS Dynamics Web Service. If I want to create a restful service in MS Dynamics through WebApi, is it mandatory that I need to have OAUTH implemented? Can create a service and hit from Java without authentication?

My another question is, is it possible to use our custom authentication method like call a another web service from MS Dynamics and validate and if authorised user then send data.

I am ok in implementing Java client but I am not familiar with MS Dynamics and not able to find any help from net.

halfer
  • 19,824
  • 17
  • 99
  • 186
Lolly
  • 34,250
  • 42
  • 115
  • 150

1 Answers1

1

Here's an example from Jason Lattimer's blog post: CRM Web API Using Java

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

Aron
  • 3,877
  • 3
  • 14
  • 21
  • 1
    This answer is overly vulnerable to link decay. Can you summarize this in a way that answers the question? – Ceribia May 31 '17 at 18:16
  • I have added the example from Jason Lattimer's blog – Aron Jun 13 '17 at 14:28
  • Great! Thanks for improving it. – Ceribia Jun 13 '17 at 18:41
  • connection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); connection.setRequestMethod("POST"); This only works if the receiving end supports it. But unfortunately Microsoft Dynamics does not support the method override. – ashish2k3 Aug 30 '17 at 21:05