Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

How retrieve Access token in bussiness central cloud

Posted on by 73

Dear All ,I'm using below code to generate Access token in business central cloud. But its not generating token. Can anyone suggest how to achieve this in business central cloud

procedure MakeRequest(): Text;
var
client: HttpClient;
request: HttpRequestMessage;
response: HttpResponseMessage;
contentHeaders: HttpHeaders;
content: HttpContent;
RspJObject: JsonObject;
AccessToken, responseText, uri : Text;
begin
// Add the payload to the content
//content.WriteFrom(payload);

//Add uri
uri := 'gsp.adaequare.com/.../authenticate

// Retrieve the contentHeaders associated with the content
content.GetHeaders(contentHeaders);
contentHeaders.Clear();
contentHeaders.Add('Content-Type', 'application/json');
contentHeaders.Add('gspappid', 'XXX');
contentHeaders.Add('gspappsecret', 'YYY');


request.Content := content;

request.SetRequestUri(uri);
request.Method := 'POST';

if client.Send(request, response) then begin

// Read the response content as json.
response.Content().ReadAs(responseText);
RspJObject.ReadFrom(ResponseText);

AccessToken := ParseAuthTokenResponse(responseText);//Parse Json Response

//AccessToken := GetJsonTextField(RspJObject, 'access_token');

exit(AccessToken);
end;
end;

Categories:
  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: How retrieve Access token in bussiness central cloud

    You can follow the below link

    robertostefanettinavblog.com/.../

  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,272 Super User 2024 Season 1 on at
    RE: How retrieve Access token in bussiness central cloud

    hi

    try this:

    local procedure AcquireToken(

           AuthorizationCode: Text;

           UserName: Text;

           Password: Text;

           ClientId: Text;

           ClientSecret: Text;

           Scope: Text;

           RedirectURL: Text;

           TokenEndpointURL: Text;

           JAccessToken: JsonObject): Boolean;

       var

           Client: HttpClient;

           Request: HttpRequestMessage;

           Response: HttpResponseMessage;

           Content: HttpContent;

           ContentHeaders: HttpHeaders;

           ContentText: Text;

           ResponseText: Text;

           IsSuccess: Boolean;

       begin

            ContentText := 'grant_type=client_credentials' +

            '&client_id=' + UriBuilder.EscapeDataString(ClientId) +

            '&client_secret=' + UriBuilder.EscapeDataString(ClientSecret) +

            '&scope=' + UriBuilder.EscapeDataString(Scope);

           Content.WriteFrom(ContentText);

           Content.GetHeaders(ContentHeaders);

           ContentHeaders.Remove('Content-Type');

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

           Request.Method := 'POST';

           Request.SetRequestUri(TokenEndpointURL);

           Request.Content(Content);

           if Client.Send(Request, Response) then

               if Response.IsSuccessStatusCode() then begin

                   if Response.Content.ReadAs(ResponseText) then

                       IsSuccess := JAccessToken.ReadFrom(ResponseText);

               end else

                   if Response.Content.ReadAs(ResponseText) then

                       JAccessToken.ReadFrom(ResponseText);

           exit(IsSuccess);

       end;

    check my answer if it helped you, thanks

    DAniele

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,228 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans