web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

How do I connect to web services in an extension?

(0) ShareShare
ReportReport
Posted on by 99

I am trying to consume an API, but I cannot authenticate to get the token. I have tried to build the URL by concatenating parameters like: client_id, client_secret, username and password. I have tested the API in Postman and it works correctly. What else should I add to the URL and how can the token be authenticated and obtained?

Result obtained in Postman

postman.jpg

Current code

procedure MyProcedure()
    var
        Client: HttpClient;
        ContentHeaders: HttpContent;
        Request: HttpRequestMessage;
        Response: HttpResponseMessage;
        Headers: HttpHeaders;
    begin

        ContentHeaders.GetHeaders(Headers);
        Headers.Clear();
        Headers.Add('Content-Type', 'application/json');
        Request.Content := ContentHeaders;
        Request.Method := 'POST';
        Request.SetRequestUri('http://pre-env.auth.linkx.global/api/v1/oauth/token'  
        'grant_type=password'  
        '&client_id=XXX'  
        '&client_secret=XXX'  
        '&username=XXX'  
        '&password=XXX');
        Request.Content(ContentHeaders);
        if Client.Send(Request, Response) then begin
            Response.Content().ReadAs(TokenValue);
        end else
            error('Could not contact api');
    end;

I have the same question (0)
  • Suggested answer
    Stefano Demiliani Profile Picture
    37,166 Most Valuable Professional on at

    It depends on the endpoint specification you're calling. Are you sure that you need to pass client_id and client_secret in the URL and maybe not in the request header? Something like:

    Headers.Add("client_id", 'XXX');

    Headers.Add("client_secret", 'XXX');

  • isabtogumon Profile Picture
    99 on at

    I really don't know if these parameters pass in the URL, I followed your recommendation and modified the code, but still can't authenticate in API. 

        local procedure MyProcedure()
        var
            Client: HttpClient;
            ContentHeaders: HttpContent;
            Request: HttpRequestMessage;
            Response: HttpResponseMessage;
            Headers: HttpHeaders;
        begin
    
            ContentHeaders.GetHeaders(Headers);
            Headers.Clear();
            Headers.Add('Content-Type', 'application/json');
            Headers.Add('client_id', 'XXX');
            Headers.Add('client_secret', 'XXX');
            Request.Content := ContentHeaders;
            Request.Method := 'POST';
            Request.SetRequestUri('http://pre-env.auth.linkx.global/api/v1/oauth/token?'  
            'grant_type=password'  
            '&username=XXX'  
            '&password=XXX');
            Request.Content(ContentHeaders);
            if Client.Send(Request, Response) then begin
                Response.Content().ReadAs(TokenValue);
            end else
                error('Could not contact api');
        end;

  • Suggested answer
    isabtogumon Profile Picture
    99 on at

    I have found the solution, I share part of my code in case someone has the same question

        local procedure OAuth()
        var
            Client: HttpClient;
            ContentHeaders: HttpContent;
            Request: HttpRequestMessage;
            Response: HttpResponseMessage;
            Headers: HttpHeaders;
        begin
            ContentHeaders.WriteFrom('{"grant_type": "password",'  
                    '"client_id": "XXX",'  
                    '"client_secret": "XXX",'  
                    '"username": "XXX",'  
                    '"password": "XXX"}'); //If all four parameters are required, the structure must be respected.
            ContentHeaders.GetHeaders(Headers);
            Headers.Clear();
            Headers.Add('Content-Type', 'application/json');
            Request.Content := ContentHeaders;
            Request.Method := 'POST';
            Request.SetRequestUri('your URL');
            Request.Content(ContentHeaders);
            if Client.Send(Request, Response) then begin
                Response.Content().ReadAs(JsonText); //I show it in a text box inside a page object
            end else begin
                error('Could not contact api');
            end;
        end;

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,926 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,158 Super User 2026 Season 1

#3
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 533 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans