具有客户端凭据OAuth流的Dynamics CRM 2016 Online Rest API

JKl*_*lug 6 azure oauth-2.0 dynamics-crm-2016

我正在尝试使用Dynamics CRM 2016 Online和Azure Active Directory进行身份验证.我能够按照这里的所有步骤:

https://msdn.microsoft.com/en-us/library/mt622431.aspxhttps://msdn.microsoft.com/en-us/library/gg327838.aspx

但这些步骤演示了如何设置用户名认证流程.我想使用客户端凭据流.我在Azure AD中创建了一个新应用程序 - 一个Web应用程序.我有一个客户端ID和一个应用程序密钥,我设置了Dynamics CRM Online的权限.我能够获得访问令牌,但在后续调用中我收到此错误:

HTTP错误401 - 未经授权:访问被拒绝

我错过了一步吗?有人知道某个帖子提供了如何让这个流程运行的详细信息吗?

这是我的代码:

        string clientId = "<client id>";
        string appKey = "<app key>";

        // Get the authority and resource URL at runtime
        AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri("https://<org address>/api/data/")).Result;
        String authorityUrl = ap.Authority;
        String resourceUrl = ap.Resource;

        // Authenticate the registered application with Azure Active Directory.
        AuthenticationContext authContext = new AuthenticationContext(authorityUrl);
        ClientCredential clientCredential = new ClientCredential(clientId, appKey);

        AuthenticationResult result = authContext.AcquireToken(resourceUrl, clientCredential);

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
        HttpResponseMessage response = client.GetAsync("https://<org address>/api/data/v8.1/EntityDefinitions").Result;
Run Code Online (Sandbox Code Playgroud)

Van*_*yen 0

您需要添加“应用程序用户”并在 CRM 中分配自定义安全角色。请参阅我的答案/sf/answers/3398839181/