Notifications
Announcements
No record found.
We are working on integrating our API's with Business Central. We need Business Central to hit our OAuth server to get the token and make the authenticated request. We are having an extremely hard time finding sample code to do this on the web. One of the only examples we found was here: https://github.com/microsoft/BCTech/blob/master/samples/OAuth2Flows/TestOAuth2Flows.Page.al
But the problem with that is there is no mention of an "audience" parameter. We need to pass an "audience" to our OAuth server to correctly authenticate. Does anyone know where we can find good relevant code to do this? Or has anyone done this and can give some direction?
Thanks!
Mike
Hi,
You can try with below code
procedure GenerateAccessToken() var APIConfiguration: Record "EzyVet API Configuration"; begin APIConfiguration.Get(); APIConfiguration.TestField(partner_id); APIConfiguration.TestField(client_id); APIConfiguration.TestField(client_secret); APIConfiguration.TestField(grant_type); APIConfiguration.TestField(scope); APIConfiguration.TestField(access_token_endpoint_url);
gbodyContent := StrSubstNo('partner_id=%1&client_id=%2&client_secret=%3&grant_type=%4&scope=%5', APIConfiguration.partner_id, APIConfiguration.client_id, APIConfiguration.client_secret, APIConfiguration.grant_type, APIConfiguration.scope); gHttpcontent.WriteFrom(gbodyContent); gHttpcontent.GetHeaders(gHttpheaders); gHttpheaders.Remove('Content-Type'); gHttpHeaders.Add('Content-Type', 'application/x-www-form-urlencoded'); if gHttpclient.Post(APIConfiguration.access_token_endpoint_url, gHttpcontent, ghttpResponseMessage) then begin ghttpResponseMessage.Content.ReadAs(gresponseText); gJSONToken.ReadFrom(gresponseText); gJSONObject := gJSONToken.AsObject(); APIConfiguration.access_token := GetJSONToken(gJSONObject, 'access_token').AsValue().AsText(); APIConfiguration.Modify(); Message('Access Token Retrieved'); end else Error(FORMAT(ghttpResponseMessage.Content));
end;
local procedure GetJSONToken(JsonObject: JsonObject; TokenKey: Text) JsonToken: JsonToken; var begin if not JsonObject.get(TokenKey, JsonToken) then Error('Could not find a token with key %1', TokenKey); end;
var ghttpClient: HttpClient; ghttpContent: HttpContent; ghttpHeaders: HttpHeaders; ghttpRequestMessage: HttpRequestMessage; ghttpResponseMessage: HttpResponseMessage; gbodyContent: Text; gresponseText: Text; gJSONToken: JsonToken; gJSONObject: JsonObject;}
https://www.navwithnav.com/oauth2-0-token-authentication-with-al-code-for-business-central/
So is the "partner_id" the same as the "audience"?
partner id is your tenant Id here.
hello
you must apply customisation
greetings
DANiele
For the Audience.
powerusers.microsoft.com/.../429639
DAnny3211 What does that even mean?
Nitin Verma That link talks about putting it in Active Directory or something and how to find out what the audience is. I know what the audience is. I need to figure out how to pass in the call to get the auth token in code, in a .ai CodeUnit. I'm sure we are not the first ones to ever do this.
hitry constructing the json body like this
Thank you DAnny3211 . Are you able to show the code where the actual call is made? Does it use oauth2.acquiretokenwithclientcredentials?
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
OussamaSabbouh 3,010
Jainam M. Kothari 1,270 Super User 2025 Season 2
YUN ZHU 1,085 Super User 2025 Season 2