Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

Passing the audience when getting OAuth token

(0) ShareShare
ReportReport
Posted on by 8

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

  • mdodge Profile Picture
    mdodge 8 on at
    RE: Passing the audience when getting OAuth token

    Thank you DAnny3211 .  Are you able to show the code where the actual call is made?  Does it use oauth2.acquiretokenwithclientcredentials?

  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,274 Moderator on at
    RE: Passing the audience when getting OAuth token

    hi
    try constructing the json body like this

    pastedimage1667232195611v1.png

    DANiele

  • mdodge Profile Picture
    mdodge 8 on at
    RE: Passing the audience when getting OAuth token

    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.

  • mdodge Profile Picture
    mdodge 8 on at
    RE: Passing the audience when getting OAuth token

    DAnny3211 What does that even mean?

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,307 Moderator on at
    RE: Passing the audience when getting OAuth token

    For the Audience.

    powerusers.microsoft.com/.../429639

  • DAnny3211 Profile Picture
    DAnny3211 9,274 Moderator on at
    RE: Passing the audience when getting OAuth token

    hello

    you must apply customisation

    greetings

    DANiele

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,307 Moderator on at
    RE: Passing the audience when getting OAuth token

    partner id is your tenant Id here.

  • mdodge Profile Picture
    mdodge 8 on at
    RE: Passing the audience when getting OAuth token

    So is the "partner_id" the same as the "audience"?

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,307 Moderator on at
    RE: Passing the audience when getting OAuth token

    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/

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.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,407 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans