简体   繁体   English

Java连接Microsoft Dynamics 365

[英]java to connect Microsoft dynamics 365

I need to connect Microsoft dynamics 365 CRM using java, I can only see this( https://msdn.microsoft.com/en-us/library/jj602979(v=crm.5).aspx ) link for the java connection. 我需要使用Java连接Microsoft Dynamics 365 CRM,对于Java连接,我只能看到此( https://msdn.microsoft.com/zh-cn/library/jj602979(v=crm.5).aspx )链接。 Can anyone please tell me how to connect ms dynamics using java. 谁能告诉我如何使用java连接ms动态。

My only need is to load the contacts from CRM to my appplication. 我唯一需要的是将联系人从CRM加载到我的应用程序中。

You should use the WebAPI . 您应该使用WebAPI

The Web API, which is new for Microsoft Dynamics 365 (online & on-premises), provides a development experience that can be used across a wide variety of programming languages, platforms, and devices. Web API是Microsoft Dynamics 365(在线和本地)的新增功能,可提供可在多种编程语言,平台和设备上使用的开发体验。 The Web API implements the OData (Open Data Protocol), version 4.0, an OASIS standard for building and consuming RESTful APIs over rich data sources. Web API实现了OData(开放数据协议)版本4.0,这是用于通过丰富数据源构建和使用RESTful API的OASIS标准。

Via the link: Connection and data exchange, Java with Microsoft Dynamics you get an extensive tutorial for connecting from Java with 'Dynamics', using ADFS and OAuth2. 通过链接: 连接和数据交换,Java与Microsoft Dynamics,您将获得有关使用ADFS和OAuth2从Java与“ Dynamics”进行连接的详尽教程。 All details are there. 所有细节都在那里。 Also the READ and WRITE operations are implemented, in the tutorial. 本教程还实现了READ和WRITE操作。

The implementation makes use of the Java-library OLingo for exchanging the actual read- and write OData messages, via the http-endpoint of the Dynamics-application. 该实现利用Java库OLingo通过Dynamics应用程序的http端点交换实际的读写OData消息。

You can use my GitHub example . 您可以使用我的GitHub示例 I use adal4j to take care of all the auth overhead and it gives me a bearer token that I can use to make api calls to my organization. 我使用adal4j来处理所有身份验证开销,它为我提供了一个承载令牌,可用于对组织进行api调用。

import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
import okhttp3.*;

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 Main {
    public static void main(String[] args) {

        String authority = "https://login.microsoftonline.com/";
        String resource = "https://msott.crm.dynamics.com";
        String clientId = "64f4cba8-0656-4ccd-8c2a-fd269fe7636f";
        String clientSecret = "";
        String tenantID = "grdegr.onmicrosoft.com";
        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();

            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");
            RequestBody body = RequestBody.create(mediaType, "{" +
                    "\"name\": \"Sample Postman Account\"," +
                    "\"creditonhold\": false," +
                    "\"address1_latitude\": 47.639583," +
                    "\"description\": \"This is the description of the sample account\"," +
                    "\"revenue\": 5000000," +
                    "\"accountcategorycode\": 1" +
                    "}");
            Request request = new Request.Builder()
                    .url("https://msott.api.crm.dynamics.com/api/data/v9.0/accounts?$select=name,creditonhold,address1_latitude,description,revenue,accountcategorycode,createdon")
                    .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", "return=representation")
                    .addHeader("Authorization", "Bearer " + accessToken)
                    .addHeader("cache-control", "no-cache")
                    .addHeader("Postman-Token", "472f1651-c4e1-47c1-8a5c-6f70636181b0")
                    .build();

            Response response = client.newCall(request).execute();

            String dataReturnedFromCreate = response.body().string();

            System.out.println();
        }
        catch (IOException e) { }
    }
}

I'm using these Maven packages 我正在使用这些Maven软件包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mrrobot</groupId>
    <artifactId>dynamicscrmapi</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <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>
    </dependencies>
</project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM