2017-03-29 29 views
1

我有一个msdynamics CRM线索帐户,注册我的CRM在Azure中的Active Directory为Web应用程序/ API和URL为http://localhost,我给了允许为了动态CRM在线作为检查的委托允许。女士动态的Web API 401未授权

下面是我使用来获取代码的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未授权。谁能帮帮我吗?

+1

的WebAPI真的打算从里面CRM使用。从外部你最好使用Xrm.Tooling.Connector(ex-Xrm.Client) – Alex

+0

但我需要通过rest api连接它,我不应该使用soap端点我通过分配角色和添加来解决这个问题这个AAD客户端ID到CRM的应用程序用户。但现在的任务是我不应该使用Azure ClientID和ClientSecret,而是使用CRM凭证(用户名/密码)与CRM连接。任何帮助将被赞赏.. – Jagan

回答

1

我想因为你的配置有问题。

我有完整的答案在另一个计算器后,请检查我的回答后:https://stackoverflow.com/a/43164164/1063168

+0

谢谢你,我想出了配置问题,我需要将客户端ID添加到CRM的用户。我需要知道是否有任何可能的方式通过仅使用CRM凭证(即用户名和密码)和没有AAD clientId和ClientSecret来连接Dynamics CRM。 – Jagan