java连接Microsoft Dynamics 365

时间:2017-03-16 06:01:43

标签: java dynamics-crm microsoft-dynamics

我需要使用java连接Microsoft Dynamics 365 CRM,我只能看到这个(https://msdn.microsoft.com/en-us/library/jj602979(v=crm.5).aspx)链接用于java连接。任何人都可以告诉我如何使用java连接ms动力学。

我唯一需要的是将CRM中的联系人加载到我的应用程序中。

3 个答案:

答案 0 :(得分:2)

您应该使用WebAPI

  

Web API,是Microsoft Dynamics 365的新增功能(在线&   本地),提供可以使用的开发体验   跨越各种编程语言,平台和   设备。 Web API实现OData(开放数据协议),   版本4.0,用于构建和使用RESTful API的OASIS标准   超过丰富的数据源。

答案 1 :(得分:0)

通过链接: Connection and data exchange, Java with Microsoft Dynamics您将获得使用ADFS和OAuth2从Java与“ Dynamics”进行连接的详尽教程。所有细节都在那里。本教程中还实现了READ和WRITE操作。

该实现利用Java库OLingo通过Dynamics应用程序的http端点交换实际的读写OData消息。

答案 2 :(得分:0)

您可以使用我的GitHub example。我使用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) { }
    }
}

我正在使用这些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>