string organizationUrl = "<org url>";string clientId = "<client id in Azure>";string appKey = "<secret for the clientid>";string aadInstance = "https://login.microsoftonline.com/";string tenantID = "<contoso or whateveryouhave>.onmicrosoft.com";
ClientCredential clientcred = new ClientCredential(clientId, appKey);AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID);AuthenticationResult authenticationResult = authenticationContext.AcquireToken(organizationUrl, clientcred);var requestedToken = authenticationResult.AccessToken;
using (OrganizationWebProxyClient sdkService = new OrganizationWebProxyClient(GetServiceUrl(organizationUrl), false)){sdkService.HeaderToken = requestedToken;OrganizationRequest request = new OrganizationRequest();
WhoAmIResponse response = sdkService.Execute(new WhoAmIRequest()) as WhoAmIResponse;
Console.WriteLine($"UserID:{response.UserId}");Console.WriteLine("Press any key to continue...");Console.ReadKey();
}
However, the line:
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(organizationUrl, clientcred);
Cloud computing - isn't it great? |
If you look around the net, many advocate staying on 2.x version of the library but I didn't have that option as I was integrating with Dyn 365 Operations and that was using the ver 3.x.
So, to make this work in version 3.x you just have to make a simple fix:
AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(ClientConfig.CRMOrgUrl, clientcred).Result;
Gustaf Westerlund
MVP, Founder and CTO at CRM-konsulterna AB
www.crmkonsulterna.se
No comments:
Post a Comment