We are evaluating usage of Web API for our Dynamics CRM 2016 Online integrations. I am attempting to configure a local console application to interface with CRM data using the Web API with OAuth authentication. I have the authentication supposedly working (at least to get th e access token), but anytime I attempt to access data (via a Microsoft OData client or straight Web API HTTP requests), I always receive a 401, despite the inclusion of my access token in the authorization header. I have registered my application similarly to this guide using a trial instance of CRM Online:
http://mscrmshop.blogspot.in/2016/01/crm-web-api-and-cors-support.html
Below is my code (the last line with execute() receives the 401). Your help is appreciated. Thanks!
string resource = "https://myorganization.crm.dynamics.com";
string clientId = "guid of my client id defined in azure";
string clientSecret = "secret value from azure";
// Authenticate the registered application with Azure Active Directory.
// get this URL from azure directory endpoints modal
AuthenticationContext authContext =
new AuthenticationContext("https://login.microsoftonline.com/my tenant id guid", false);
ClientCredential clientCredentials = new ClientCredential(clientId, clientSecret);
AuthenticationResult authResult = authContext.AcquireToken(resource, clientCredentials);
string accessToken = authResult.AccessToken;
UnitTestProject2.Microsoft.Dynamics.CRM.System system = new Microsoft.Dynamics.CRM.System(new Uri("https://myorganization.api.crm.dynamics.com/api/data/v8.0/"));
// Register to handle the SendingRequest event.
// Note: If this must be done for every request to the data service, consider
// registering for this event by overriding the OnContextCreated partial method in
// the entity container, in this case NorthwindEntities.
system.SendingRequest2 += (sender, eventArgs) =>
{
eventArgs.RequestMessage.SetHeader("Authorization", "Bearer " + accessToken);
};
Console.Write(system.Accounts.Execute().First().Name);
*This post is locked for comments
I have the same question (0)