如何针对 Dynamics 365 数据导出服务 API 进行身份验证?

How do I authenticate against the Dynamics 365 Data Export Service API?

我为 Dynamics 365 设置了一个名为 Data Export Service 的东西,以便它复制到 Azure SQL 数据库中。这按预期工作。

我正在尝试找到一种方法,以便在该服务遇到任何错误时主动收到通知。似乎没有通过 CRM 本身的设置来执行此操作的本机方法,but they do provide an API. The Swagger page outlining all methods can be found here

我正在尝试使用 Postman 调用 GetProfilesByOrganizationId 方法:

https://discovery.crmreplication.azure.net/crm/exporter/profiles?organizationId=4ef7XXXX-XXXX-XXXX-XXXX-XXXXXX8a98f&status=true

我遇到身份验证问题并且总是收到以下错误:

"Message": "Received unauthenticated requestRequest Url https://discovery.crmreplication.azure.net/crm/exporter/profiles?organizationId=4ef7XXXX-XXXX-XXXX-XXXX-XXXXXX8a98f&status=true"

我已经在 Azure 中注册了一个应用程序,该应用程序有权代表经过身份验证的用户访问 Dynamics 365,在本例中是我,即管理员。

我在 Postman 的授权选项卡上将类型设置为 OAuth 2.0。我已经成功地针对上述应用程序使用授权代码的授权类型请求了一个访问令牌。这已将 header 添加到请求中:

Key: Authorization
Value: Bearer BIGLONGACCESSTOKEN

尽管 header 存在,但我仍然收到上述错误。

API 文档暗示身份验证是 OAuth2 Implicit Grant Flow(单击文档中的任何红色感叹号),但我无法在 Postman 中使用它。当我尝试使用此方法请求令牌时出现错误:

unsupported_response_type

...在 Postman 控制台中。

关于如何在 Postman 中针对此 API 进行身份验证(使用隐式授权?)的任何想法?

(如果 C# 示例更合适,我会接受它们,但如果 Postman 不能告诉我我需要什么,我会感到惊讶)

看起来 Microsoft 显示的代码示例可以 如果使用较新的方法更新并在 Azure 中使用一些未记录的额外配置,则可以工作。

Azure 配置

通过安装数据导出服务(假设一切正常),您将在 Azure AD 中作为 Crm Exporter.

列出一个新的企业应用程序

要利用此应用程序并通过数据导出进行身份验证API,您必须配置自己的应用程序。

转到 Azure AD 中的应用程序注册选项卡并添加新的应用程序注册。
为其命名并将应用程序类型设置为本机。只要重定向 URI 有效,它通常并不重要。

单击清单按钮编辑清单,将 属性 oauth2AllowImplicitFlow 更改为 true 并保存更改。

唯一的其他重要配置是必需的权限,应设置如下:

  • Windows Azure 活动目录
    • 委派权限
      • 登录并阅读用户个人资料
  • Microsoft Dynamics 365 (Crm Exporter) 的数据导出服务
    • 委派权限
      • 有权访问 Microsoft Dynamics 365 的数据导出服务API

然后您需要单击授予权限

C# 变化

更新后的方法如下所示:

using Microsoft.IdentityModel.Clients.ActiveDirectory;

string clientId = "11cfXXXX-XXXX-XXXX-XXXX-XXXXXXXXd020";
string user = "my.username@domain.com";
string password = "PASSWORD";

var authParam= await AuthenticationParameters.CreateFromResourceUrlAsync(
    new Uri("https://discovery.crmreplication.azure.net/crm/exporter/aad/challenge")
);

var context = new AuthenticationContext(authParam.Authority, false);

var credentials = new UserPasswordCredential(user, password);

var token = await context.AcquireTokenAsync(authParam.Resource, clientId, credentials).AccessToken;

您现在可以通过提供令牌作为 header:

来查询数据导出 API

Authorization : Bearer eJ0y........Hgzk

curl -X GET --header 'Accept: application/json' 'https://discovery.crmreplication.azure.net/crm/exporter/profiles?organizationId=MyOrgId&status=true'