从JSON中的Dynamics CRM 365获取数据

时间:2018-07-13 13:19:24

标签: c# odata dynamics-crm-online

我需要从Dynamics CRM 365在线获取数据。有人尝试过吗?

我需要知道我需要哪种信息(clientid,clientsecret),以通过c Sharp连接并将数据(JSON)保存到例如平面文件中。

编辑: 如果需要使用非异步方法,请使用ADAL.Net v2。 请记住,将令牌放在“授权”下的请求标头中。

1 个答案:

答案 0 :(得分:1)

您需要使用OAuth从您的C#代码向Dynamics 365 Online进行身份验证。

// TODO Substitute your correct CRM root service address,   
string resource = "https://mydomain.crm.dynamics.com";  

// TODO Substitute your app registration values that can be obtained after you  
// register the app in Active Directory on the Microsoft Azure portal.  
string clientId = "e5cf0024-a66a-4f16-85ce-99ba97a24bb2";  
string redirectUrl = "http://localhost/SdkSample";  

// Authenticate the registered application with Azure Active Directory.  
AuthenticationContext authContext =   
    new AuthenticationContext("https://login.windows.net/common", false);  
AuthenticationResult result = authContext.AcquireToken(resource, clientId, new Uri(redirectUrl));  

然后您可以使用AuthenticationResult通过HttpClient发出HTTP请求:

using (HttpClient httpClient = new HttpClient())  
{  
    httpClient.Timeout = new TimeSpan(0, 2, 0);  // 2 minutes  
    httpClient.DefaultRequestHeaders.Authorization =   
        new AuthenticationHeaderValue("Bearer", result.AccessToken); 
//TODO Implement your WebApi calls
}

这些代码示例和其他详细信息,包括如何在Azure AD中注册应用程序,位于以下链接中:https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/connect-customer-engagement-web-services-using-oauth