Skip to main content

Notifications

Community site session details

Community site session details

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

Calling an API in OAuth with grant type Authorization Code from Business Central

(0) ShareShare
ReportReport
Posted on by 71

Hi everyone,

I'm currently trying to call an Oauth Business Central API from Business Central with a grant type authorization code.

I've set up an On Premise business central base with OAuth authentification  by following every steps on this documentation https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/administration/register-app-azure (registering in Azure etc.)

The OAuth setup works well in Postman. I've setup the authentification as Authorization code, and the flow to obtain the security token works fine

pastedimage1683881827050v1.png 

I've exposed some custom API on my base and everything works well.

My issue appears when I'm trying to retrive the security token to call these same APIs in AL. I have issues that I don't seem to find solutions to with the grant type used here.

I have tried first to retrive my security token using the procedure AcquireTokenByAuthorizationCode in codeunit 501 "OAuth2" by sending the same info I'm sending in postman

pastedimage1683882139905v2.png

But I only get this response back 

pastedimage1683882157422v3.png

(the sentence in french says Sorry we're having troubles to connect you).

The part about the request body seems strange to me as it looks like a bad request construction, but since I don't have any control on how the request is set up, it looks like the procedure simply does not work ? 

I've also tried to create my own request to retrieve the authorization token, but when the return of my request should have an http code 302 and open a browser for me to authentificate into my azure account, i only get an http 200 with the page's html code in the response

pastedimage1683882572663v4.png

Has anyone done this before ? 

Thanks

  • Valentine Motin Profile Picture
    71 on at
    RE: Calling an API in OAuth with grant type Authorization Code from Business Central

    Thank you so much Ramiz , this solved my problem !

  • Verified answer
    Ramiz Profile Picture
    589 on at
    RE: Calling an API in OAuth with grant type Authorization Code from Business Central

    Hi Valentine Motin 

    Kindly check the Azure Active Directory Applications in BC if your app has been configured there and the values that you are passing are correct.

    pastedimage1683894245610v1.png

  • Valentine Motin Profile Picture
    71 on at
    RE: Calling an API in OAuth with grant type Authorization Code from Business Central

    Hi Ramiz

    Thank you for your answer,

    Unfortunately when I try "AcquireTokenWithClientCredentials" i get a 401 error "The server has rejected the client credentials."

    Do you think it could be due to azure registration ?

  • Suggested answer
    Ramiz Profile Picture
    589 on at
    RE: Calling an API in OAuth with grant type Authorization Code from Business Central

    Hi Valentine Motin 

    Try using this. It works for me.

    var
        OAuth2: Codeunit OAuth2;
    
    OAuth2.AcquireTokenWithClientCredentials(ClientID, ClientSecret, OAuthURL, RedirectURL, Scopes, Accesstoken);
    Headers.Add('Authorization', StrSubstNo('Bearer %1', Accesstoken));

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics

Product updates

Dynamics 365 release plans