Hello Readers,
I started working on CRM Odata Intergartion with Business Central and found it very difficult, there were no proper blog from where i could get the help/idea, i have spent so much time to get it working through AL solution.
Below is the working solution with the Odata integration with Business central, which will help you to make the solution ready with less effort.
In this part i will show you how to get the authentication token.
- GetOauthToken
To get the authentication token below prerequisite are required.
a. Token URL - below will be the URL, you can use common as well as your tenant id.
https://login.microsoftonline.com/common/oauth2/token
or
https://login.microsoftonline.com/{tenant}/oauth2/token
b. Client Id - Get it from the app registration on azure.portal.com
c. Client Secret - While registering the app you will get the secret id, copy it at the same time because it get encrypted after refreshing the page.
d. Resource - Your CRM instance URL.
e. Create Application user in CRM.
: we will use the above information in our below code for authentication token
procedure GetOauthToken()
var
TokenURL: Label 'https://login.microsoftonline.com/ad0cc5eb-XXXX-43d6-8496-c06723118d4f/oauth2/token';
ClientId: Label '2eb0ae68-a0d9-43ac-8def-946151a8XXXX';
ClientSecret: Label 'mA87Q/18XE:oU.e-=XXXXX[zXa7pG_80';
Resource: Label 'https://xxTest.crm.dynamics.com';
RequestBody: Label 'grant_type=client_credentials&client_id=%1&client_secret=%2&resource=%3';
HttpClient: HttpClient;
RequestMessage: HttpRequestMessage;
ResponseMessage: HttpResponseMessage;
RequestHeader: HttpHeaders;
Content: HttpContent;
TempBlob: Record TempBlob;
Outstr: OutStream;
Instr: InStream;
Nextbase: Text;
Token: text;
//Read Json
JsonToken: JsonToken;
JsonObject: JsonObject;
JsonArray: JsonArray;
begin
Content.GetHeaders(RequestHeader);
RequestHeader.Clear();
HttpClient.SetBaseAddress(TokenURL);
RequestMessage.Method('POST');
RequestHeader.Remove('Content-Type');
RequestHeader.Add('Content-Type', 'application/x-www-form-urlencoded');
Clear(TempBlob);
TempBlob.blob.CreateOutStream(Outstr);
Outstr.WriteText(StrSubstNo(RequestBody, ClientId, ClientSecret, Resource));
TempBlob.blob.CreateInStream(Instr);
Content.WriteFrom(Instr);
RequestMessage.Content(Content);
HttpClient.Send(RequestMessage, ResponseMessage);
if ResponseMessage.IsSuccessStatusCode() then begin
ResponseMessage.Content().ReadAs(Nextbase);
end;
Clear(JsonObject);
JsonToken.ReadFrom(Nextbase);
JsonObject := JsonToken.AsObject;
IF JsonObject.Get('access_token', JsonToken) then begin
Token := JsonToken.AsValue().AsText();
end;
Message('Token: %1', Token);
end;
in next part we will see how to use the above token and get the list of contacts from the CRM Contact Odata web service.
Thank you,
Anish Agrawal
NAV / Business Central Developer
No comments:
Post a Comment