Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

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

Posted on by 35

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!

Categories:
  • Alberto Gallen Profile Picture
    Alberto Gallen 5 on at
    RE: The request body must contain the following parameter: 'grant_type'

    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;

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,025 Moderator on at
    RE: The request body must contain the following parameter: 'grant_type'

    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);
  • Suggested answer
    Marco Mels Profile Picture
    Marco Mels on at
    RE: The request body must contain the following parameter: 'grant_type'

    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.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,564 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,651 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans