Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Answered

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

Posted on by 65

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

Categories:
  • Valentine Motin Profile Picture
    Valentine Motin 65 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
    Ramiz 155 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
    Valentine Motin 65 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
    Ramiz 155 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));

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,914 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,549 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans