Trying to build an application that will query my CRM Online instance via OData. I followed the provided instructions to register my custom web app in Windows Azure Active Directory, and am using the following code to add the necessary authentication header my web client prior to executing the query:
private async void AddAuthorizationHeader(WebRequest client)
{
client.PreAuthenticate = true;
string resourceId = "https://<myCrmOrg>.crm.dynamics.com";
string authorityUrl = "https://login.windows.net/common";
string clientId = "<clientId>";
string clientSecret = "<mySecret>";
AuthenticationContext authContext = new AuthenticationContext(authorityUrl, false);
ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resourceId, clientCredential);
client.Headers.Add("Authorization", new AuthenticationHeaderValue("Bearer", authResult.AccessToken).ToString());
}
When the code hits line that references "AcquireTokenAsync", I get the following error:
Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException was unhandled by user code
ErrorCode=unauthorized_client
HResult=-2146233088
Message=AADSTS70001: Application with identifier '<clientId>' was not found in the directory crm.dynamics.com
Trace ID: 01ff559e-0470-44d5-855f-e6f4e9f57b52
Correlation ID: cafa53bd-f536-4a1d-a85d-1a439bcc4dc6
Timestamp: 2016-05-29 05:42:52Z
Source=Microsoft.IdentityModel.Clients.ActiveDirectory
StatusCode=400
I have verified that my application is indeed registered in the AD in my Azure Subscription, that my AD is part of the same tenant as my CRM Online instance, and that the Client ID I've used in my code is accurate, compared to the AD app configuration page.
Any ideas on how to fix this?
/ck