aliva
New Coder
I am a beginner in java scripting . Requirement is to create a ticket in dynamics 365.The data is available in a relational table and i am initially writing a java code where i can manually pass the values in the json input and create a ticket.
After i pass the api and the client id ,client secret and tenant id it would generate a token and based on input of the body will create a ticket (not sure if my code is working on this functionality).
Please help on same .
After i pass the api and the client id ,client secret and tenant id it would generate a token and based on input of the body will create a ticket (not sure if my code is working on this functionality).
Java:
import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
import okhttp3.*;
import okhttp3.Response;
import okhttp3.Call;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.lang.String;
import java.util.concurrent.Future;
public class javacodedynamics365 {
public static void main(String[] args) {
String authority = "https://login.microsoftonline.com/";
String resource = "https://lenovo-nitro-uat.crm.dynamics.com";
String clientId = "";
String clientSecret = "";
String tenantID = "";
ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationResult result;
try {
AuthenticationContext context = new AuthenticationContext(authority + tenantID, true, service);
Future<AuthenticationResult> future = context.acquireToken(resource, new ClientCredential(clientId, clientSecret), null);
result = future.get();
String accessToken = result.getAccessToken();
System.out.println(accessToken);
createWithDataReturned(accessToken);
}
catch (MalformedURLException e) { }
catch (InterruptedException e) { }
catch (ExecutionException e) { }
}
// TODO: 5
// Retrieving customized responses on POST method:
public static void createWithDataReturned(String accessToken) {
try {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
System.out.println(mediaType);
RequestBody body = RequestBody.create(mediaType, "{" +
"\"Account_Alias_Request\": \"Knot Technology Solutions LLC\"" +
"\"Partner_MDM_ID\": \"PA000000900211\"" +
"}");
Request request = new Request.Builder()
.url("https://lenovo-nitro-uat.crm.dynamics.com/api/data/v9.1/lvo_DSRAccountReview")
.post(body)
.addHeader("OData-MaxVersion", "4.0")
.addHeader("OData-Version", "4.0")
.addHeader("Accept", "application/json")
.addHeader("Content-Type", "application/json; charset=utf-8")
.addHeader("Prefer", "odata.include-annotations=\"*\"")
.addHeader("Authorization", "Bearer " + accessToken)
.addHeader("cache-control", "no-cache")
// .addHeader("Postman-Token", "a3ca1261-85fe-4b57-84e0-034e0d582d46")
.build();
System.out.println(request);
Response response = client.newCall(request).execute();
String dataReturnedFromCreate = response.body().string();
System.out.println(dataReturnedFromCreate);
System.out.println();
}
catch (Exception e) { }
}
}
the above code is returning the token. But gives the error on
Request{method=POST, url=https://lenovo-nitro-uat.crm.dynamics.com/api/data/v9.1/lvo_DSRAccountReview, tags={}}
{"error":{"code":"0x0","message":"An error occurred while validating input parameters: System.ArgumentException: Stream was not readable.\r\n at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean leaveOpen)\r\n at System.IO.StreamReader..ctor(Stream stream, Encoding encoding)\r\n at Microsoft.OData.JsonLight.ODataJsonLightInputContext.CreateTextReader(Stream messageStream, Encoding encoding)\r\n at Microsoft.OData.JsonLight.ODataJsonLightInputContext..ctor(ODataMessageInfo messageInfo, ODataMessageReaderSettings messageReaderSettings)\r\n at Microsoft.OData.Json.ODataJsonFormat.CreateInputContext(ODataMessageInfo messageInfo, ODataMessageReaderSettings messageReaderSettings)\r\n at Microsoft.OData.ODataMessageReader.ReadFromInput[T](Func`2 readFunc, ODataPayloadKind[] payloadKinds)\r\n at Microsoft.Crm.Extensibility.ODataV4.CrmODataActionPayloadDeserializer.Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)\r\n at System.Web.OData.Formatter.ODataMediaTypeFormatter.ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)"}}
updated the below details in pom.xml file:
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>adal4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
Please help on same .
Last edited by a moderator: