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 to generate an access token while consuming REST API?

(0) ShareShare
ReportReport
Posted on by 625

HI ALL,

We are trying generate a JSON access token for a given REST API with Client ID and Secret Id. Please provide sample code to call and generate the JSON Access token in AL.

Can someone please explain in detail how can i achieve this through AL code?

Thanks in Advance.

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    An access token for anexternal API normally should be given to you by an authentication service.

    If instead you mean that you need to create a JSON object from AL, you can check the JsonObject and JsonArray AL objects. More info here:

    docs.microsoft.com/.../jsonobject-data-type

  • Priyanka05021986 Profile Picture
    105 on at

    Hi ,

    we have c# code as given below for authorization we want to rewrite in AL but it 

    C#:-

    var restclient = new RestClient(url);
    RestRequest request = new RestRequest("/oauth/authorize") { Method = Method.GET };
    request.AddHeader("Accept", "application/json");
    request.AddParameter("redirect_uri", redirect_uri);
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddParameter("client_id", client_id);
    request.AddParameter("client_secret", client_secret);
    request.AddParameter("response_type", "code");
    var state = Guid.NewGuid();
    request.AddParameter("state", state);
    var tResponse = restclient.Execute(request);
    Uri uri = new Uri(tResponse.Headers[2].Value.ToString());
    string code = HttpUtility.ParseQueryString(uri.Query).Get("code");
    //var code =uri.Query.Replace("?code=", "");//.ResponseUri.Query.Replace("?code=", "");
    var responseJson = AuthorizeClient(code, "authorization_code", redirect_uri);

    trying to overite in AL like this but not succeeded:-

    var
            client: HttpClient;
            content: HttpContent;
            Request: HttpRequestMessage;
            response: HttpResponseMessage;
            header: HttpHeaders;
            url: Label 'wfsnopcomdemo.azurewebsites.net/.../authorize;
        begin

            header.Add('Accept', 'application/json');
            header.Add('redirect_uri', 'http://localhost:49676/token');
            header.Add('Content-Type', 'application/x-www-form-urlencoded');
            header.Add('client_id', '4d4ff269-d333-4c10-a44d-d7bfdd05375b');
            header.Add('client_secret', '8fd012c1-226c-45ef-a5f6-8eb0d6418508');
            header.Add('response_type', 'code');
            header.Add('state', 'test27may2020');
            //Request.Method('GET');
            content.GetHeaders(header);
            //client.SetBaseAddress(url);
            client.Get(url, response);
  • Pons J. Llaneras Profile Picture
    52 on at

    You have some problems in your code:

    • You consider parameters as part of header, and this is not correct
    • You don't connect the headers/content with the GET
    • You do GET, and you need POST

    I think this likely what you need:

    local procedure GetToken(Url: Text; client_id: Text; client_secret: Text) myToken: Text
    var
    	MyHttpResponseMessage: HttpResponseMessage;
    	MyHttpClient: HttpClient;
    	MyHttpContent: HttpContent;
    	myHttpHeaders: HttpHeaders;
    	myContent: Text;
    	myJson: Text;
    begin
    	myContent := strsubstno('{"client_id"="%1","client_secret"="%2',"response_type"="code"}', client_id, client_secret);
    
    	myHttpClient.DefaultRequestHeaders().Add('accept', 'application/json');
    	myHttpClient.DefaultRequestHeaders().Add('charset', 'UTF-8');
    
    	myHttpContent.WriteFrom(myContent);
    	myHttpContent.GetHeaders(myHttpHeaders);
    	if myHttpHeaders.Contains('Content-Type') then
    		myHttpHeaders.Remove('Content-Type');
    	myHttpHeaders.Add('Content-Type', 'application/x-www-form-urlencoded');
    
    	MyHttpClient.Post(Url, MyHttpContent, MyHttpResponseMessage);
    	myHttpResponseMessage.Content().ReadAs(myJson);
    	// you have to read myJson as Json and obtain de token from it and save to myToken
    end;

    This needs a little more work, but I think you will have almost what you need.

  • Priyanka05021986 Profile Picture
    105 on at

    Hi,

    Thank you the reply, but actually we have to call Oauth 2.0 with "Authorization code" calling . In this first we have request for Code that will in GET request and than POSTfor access token . The authorization code will come in reponse uri of GET calling , then we will extact it and again pass to token uri for access token.

    www.oauth.com/.../

  • Pons J. Llaneras Profile Picture
    52 on at

    Have you test something like that:

    local procedure GetToken(Url: Text; client_id: Text; client_secret: Text) myToken: Text
    var
    	MyHttpResponseMessage: HttpResponseMessage;
    	MyHttpClient: HttpClient;
    	myHttpHeaders: HttpHeaders;
    	myContent: Text;
    	myResponse: Text;
    begin
    	Url  = strsubstno('?client_id=%1&client_secret=%2&response_type=code&state=%3', client_id, client_secret, CreateGuid());
    
    	myHttpClient.DefaultRequestHeaders().Add('accept', 'application/json');
    	myHttpClient.DefaultRequestHeaders().Add('charset', 'UTF-8');
    	myHttpClient.DefaultRequestHeaders().Add('Content-Type', 'application/x-www-form-urlencoded');
    
    	MyHttpClient.Get(Url, MyHttpResponseMessage);
    	myHttpResponseMessage.Content().ReadAs(myResponse);
    	// you have to read myResponse as Json and obtain de token from it and save to myToken
    end;

    It's a variation from my previous code.

  • Priyanka05021986 Profile Picture
    105 on at

    Hi,

    I have tried but getting but getting bellow error

    Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

  • Pons J. Llaneras Profile Picture
    52 on at

    Try this way:

    local procedure GetToken(Url: Text; client_id: Text; client_secret: Text) myToken: Text
    var
    	MyHttpResponseMessage: HttpResponseMessage;
    	MyHttpClient: HttpClient;
    	myHttpHeaders: HttpHeaders;
    	myContent: Text;
    	myResponse: Text;
    begin
    	Url  = strsubstno('?client_id=%1&client_secret=%2&response_type=code&state=%3', client_id, client_secret, CreateGuid());
    
    	myHttpHeaders := MyHttpClient.DefaultRequestHeaders();
    	myHttpHeaders.Add('accept', 'application/json');
    	myHttpHeaders.Add('charset', 'UTF-8');
    	if myHttpHeaders.Contains('Content-Type') then
    		myHttpHeaders.Remove('Content-Type');
    	myHttpHeaders.Add('Content-Type', 'application/x-www-form-urlencoded');
    
    	MyHttpClient.Get(Url, MyHttpResponseMessage);
    	myHttpResponseMessage.Content().ReadAs(myResponse);
    	// you have to read myResponse as Json and obtain de token from it and save to myToken
    end;

    I think this is close to what you need.

  • Priyanka05021986 Profile Picture
    105 on at

    Hi ,

    we are still getting the same error

    Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

    whereas when we are using postman for generating the access token and even our .net c# code is also working fine

    pastedimage1590576649498v2.png

  • Pons J. Llaneras Profile Picture
    52 on at

    Probably the problem is the "content-type" part... remove references of content-type (there's no content in fact) and try again.

  • Priyanka05021986 Profile Picture
    105 on at

    Hi,

    I have removed that content part ,now getting 404 error.

    pastedimage1590580918244v1.png

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 633 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 419 Super User 2026 Season 2

#3
Teagen Boll Profile Picture

Teagen Boll 236 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans