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

The request body must contain the following parameter: 'grant_type'

(0) ShareShare
ReportReport
Posted on by 47

I want to get access token:

I already did all steps to get client_id, client_secret and ressource and with grant_type as client_credentials and sent them all to get access token.

I test that on postman and work as expected and return that access token which I want to use dealing with sharepoint.

postman.png

token.png

 

I want to simulate that request with AL code 

But won't work and show me that body must contain the following parameter: 'grant_type'

GRANT_5F00_TYPE.png

even though I only just send that grant_type

code:

    procedure GetToken()
    var
        Request: HttpRequestMessage;
        Response: HttpResponseMessage;
        Client: HttpClient;
        Content: HttpContent;
        Headers: HttpHeaders;
        contentJSON: Text;
        myJson: Text;
        link: Text;
        MyJobject: JsonObject;
        PayloadOutStream: OutStream;
        PayloadInStream: InStream;
    begin
        link := 'https://accounts.accesscontrol.windows.net/c________________________b/tokens/OAuth/2';
        Headers.Clear();
        Request.GetHeaders(Headers);
        // Headers.Add('Content-Type', 'multipart/form-data');
        Request.SetRequestUri(link);
        Request.Method := 'POST';
        contentJSON := '{"grant_type":"client_credentials"}';
        Message(contentJSON);
        Content.WriteFrom(contentJSON);
        Request.Content := Content;
        Client.Send(Request, Response);
        if Response.IsSuccessStatusCode() then
            Message('Yes');
        Response.Content().ReadAs(myJson);
        Message(myJson);
    end;

Any help is much appreciated!

I have the same question (0)
  • Suggested answer
    Marco Mels Profile Picture
    on at

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist. If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    www.dynamicsuser.net/.../14

    I will open this up to the community in case they have something to add.

    Thanks.

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

    Hi,

    Please match all your Header property with below.

     Bearer := 'Bearer ' + BearerToken;
            lHeaders.Clear();
            lContent.GetHeaders(lHeaders);
            lHeaders.Remove('Content-Type');
            lHeaders.Add('Content-Type', 'application/json');
            lreqHeaders := lClient.DefaultRequestHeaders();
            lreqHeaders.Add('Authorization', Bearer);
            lreqHeaders.Remove('Accept');
            lreqHeaders.Add('Accept', 'application/json');
            lRequest.GetHeaders(lReqHeaders);
            lContent.GetHeaders(lHeaders);
            lRequest.Method := 'POST';
            lRequest.SetRequestUri(lUrl);
            lRequest.GetHeaders(lReqHeaders);
  • Alberto Gallen Profile Picture
    5 on at

    Hi

    In this example i changed formdata by x-www-form-urlencoded, and HttpClient.Send() by HttpClient.Post()

    See the code, this example get token for a Library Document al SharePoint Site works for me, maybe it will help you

    procedure CallGetResponseToken(): Text

       var

           HttpClient: HttpClient;

           HttpContent: HttpContent;

           HttpResponseMessage: HttpResponseMessage;

           HttpHeaders: HttpHeaders;

           Url: Text;

           formdata: Text;

           ResponseText: Text;

           StatusCodeErr: Label 'Status code: %1\ Description: %2 \%3', comment = 'ESP="Status code: %1\ Description: %2 \%3"';

       begin

           Url := Url_Oauth2Token();

           formdata := FormData_Oauth2Token(SPLDSite::PurchDocuments);

           HttpContent.WriteFrom(formdata);

           HttpContent.GetHeaders(HttpHeaders);

           HttpHeaders.Clear();

           HttpHeaders.Add('Content-Type', 'application/x-www-form-urlencoded');

           HttpClient.Post(Url, HttpContent, HttpResponseMessage);

           If not HttpResponseMessage.IsSuccessStatusCode() then begin

               HttpResponseMessage.Content.ReadAs(formdata);

               Error(StatusCodeErr, HttpResponseMessage.HttpStatusCode(), HttpResponseMessage.ReasonPhrase(), formdata);

           end;

           HttpResponseMessage.Content().ReadAs(ResponseText);

           exit(ResponseText);

       end;

       local procedure Url_Oauth2Token() UrlOauth2Token: Text

       var

           SPLDSetup: Record SPLDSetup;

           TenantID: Text;

       begin

           SPLDSetup.Get(SPLDSite::None);

           SPLDSetup.TestField(TenantID);

           SPLDSetup.TestField(UrlOAuth2Token);

           TenantID := SPLDSetup.TenantID;

           UrlOauth2Token := StrSubstNo(SPLDSetup.UrlOAuth2Token, TenantID);

       end;

       local procedure FormData_Oauth2Token(_SPLDSite: Enum SPLDSite) FormDataOauth2Token: Text

       var

           SPLDSetup: Record SPLDSetup;

           ClientID: Text;

           ClientSecret: Text;

           scope: Text;

           grant_type: Text;

           tb: TextBuilder;

       begin

           SPLDSetup.Get(_SPLDSite);

           SPLDSetup.TestField(ClientID);

           SPLDSetup.TestField(ClientSecret);

           SPLDSetup.TestField(UrlOAuth2Scope);

           scope := SPLDSetup.UrlOAuth2Scope;

           grant_type := 'client_credentials';

           ClientID := SPLDSetup.ClientID;

           ClientSecret := SPLDSetup.ClientSecret;

           tb.AppendLine('grant_type=' + grant_type);

           tb.AppendLine('&' + 'client_id=' + ClientID);

           tb.AppendLine('&' + 'client_secret=' + ClientSecret);

           tb.Append('&' + 'scope=' + scope);

           FormDataOauth2Token := tb.ToText();

       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

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,226

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 2,047 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,257 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans