动态女士Web API 401未经授权

时间:2017-03-29 12:26:54

标签: dynamics-crm adal

我有一个msdynamics crm跟踪帐户,我在Azure Active Directory中将我的Crm注册为 web app / api ,将网址注册为http://localhost,我允许动态CRM在线检查委托权限。

以下是我用于获取 access_token

的代码
private static AuthenticationResult getAccessTokenFromUserCredentials() throws Exception {
    AuthenticationContext context = null;
    AuthenticationResult result = null;
    ExecutorService service = null;
    try {
        service = Executors.newFixedThreadPool(1);
        context = new AuthenticationContext(AUTHORITY, false, service);
        /*
         * Replace {client_id} with ApplicationID and {password} with
         * password that were used to create Service Principal above.
         */
        ClientCredential credential = new ClientCredential(CLIENT_ID,CLIENT_SECRET);

        Future<AuthenticationResult> future = context.acquireToken("https://XXXXXXXX.api.crm8.dynamics.com",
                credential, null);
        result = future.get();
    } finally {
        service.shutdown();
    }
    if (result == null) {
        throw new ServiceUnavailableException("authentication result was null");
    }
    return result;
}

现在我收到 access_token 以及我用来加载潜在客户的代码..

 URL url = new URL(RESOURCE + "/api/data/v8.0/opportunities?$select=name,&$expand=parentcontactid($select=contactid,firstname,lastname,jobtitle,company,emailaddress1,telephone1,telephone2)&$filter=statecode%20eq%200");
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Authorization", "Bearer " + token);
        connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
        connection.setRequestProperty("OData-MaxVersion", "4.0");
        connection.addRequestProperty("OData-Version", "4.0");


        int responseCode = connection.getResponseCode();
        System.out.println("res code : "+ responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

但我收到的响应代码 401 Unauthorized 。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

我认为因为您的配置存在问题。

我在另一篇文章中给出了完整的答案,请查看我的答案帖子:https://stackoverflow.com/a/43164164/1063168