【问题标题】:How to post data to Dynamics CRM in C# using Access Token如何使用访问令牌在 C# 中将数据发布到 Dynamics CRM
【发布时间】:2018-06-27 09:08:00
【问题描述】:

我需要将数据从 MVC 表单发布到我们的 Dynamics CRM,我有获取访问令牌的功能设置,但我不知道如何使用它将数据发送到 CRM。我可以通过 Postman 在 CRM 中发布和创建记录,但我不知道如何在 C# 中将 Access Token 和 Postman 帖子结合在一起。

获取访问令牌:

string organizationUrl = "https://myorgcrm.crm4.dynamics.com";
string appKey = "XXXXXXxxxXxXXXxXXXXXX+XXxxxxXXxxxXXxxxXXXXX=";
string aadInstance = "https://login.microsoftonline.com/";
string tenantID = "myorg.onmicrosoft.com";
string clientId = "XXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX";   

public string AuthenticateWithCRM()
{ 
    AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/common", false);

    ClientCredential clientcred = new ClientCredential(clientId, appKey);
    AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID);
    AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(organizationUrl, clientcred).Result;

    return authenticationResult.AccessToken;
}

这会返回一个访问令牌,因为它会返回一个令牌,我认为这是正确的,但在我知道如何尝试发送数据之前,我一直无法确定。

邮差……

JSON:

{
    "org_accountnumber": '12345',
    "org_Individualid":
    {
        "firstname": "Luke",
        "lastname": "Skywalker"
    },
    "orgstuff@odata.bind":"/orgstuff_schemes(XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX)"
}

我使用填写 Auth URL、Access Token URL 和 Client ID 的访问令牌运行它,然后它基于我的工作 Windows 用户帐户工作。

【问题讨论】:

    标签: c# asp.net-mvc post dynamics-crm crm


    【解决方案1】:

    对 Dynamics CRM 不太熟悉,但我 found on the MS DOCS 似乎很熟悉。

    通常,当用户登录您的应用程序或您的应用程序启动时,您会请求 accessToken。根据 OAuth,您必须将 accesstoken 作为任何请求的标头传递。在 C# 中执行此操作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 2012-08-13
      • 1970-01-01
      • 2012-02-13
      • 2022-12-16
      相关资源
      最近更新 更多