web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Suggested answer
    Nitin Verma Profile Picture
    21,708 Moderator on at

    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/

  • mdodge Profile Picture
    8 on at

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

  • Suggested answer
    Nitin Verma Profile Picture
    21,708 Moderator on at

    partner id is your tenant Id here.

  • DAnny3211 Profile Picture
    11,397 on at

    hello

    you must apply customisation

    greetings

    DANiele

  • Suggested answer
    Nitin Verma Profile Picture
    21,708 Moderator on at

    For the Audience.

    powerusers.microsoft.com/.../429639

  • mdodge Profile Picture
    8 on at

    DAnny3211 What does that even mean?

  • mdodge Profile Picture
    8 on at

    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.

  • Suggested answer
    DAnny3211 Profile Picture
    11,397 on at

    hi
    try constructing the json body like this

    pastedimage1667232195611v1.png

    DANiele

  • mdodge Profile Picture
    8 on at

    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.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,010

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,270 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,085 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans