procedure GetToken() ResponseText: text; Var client: HttpClient; cont: HttpContent; header: HttpHeaders; response: HttpResponseMessage; Jobject: JsonObject; tmpString: Text; TypeHelper: Codeunit "Type Helper"; granttype: text; clienid: text; username: text; password: text; Begin granttype := 'xxxxx'; clienid := 'xxxxxx'; username := 'xxxxxxxxx'; password := 'xxxxxxxxxxxxx'; Jobject.Add('grant_type', TypeHelper.UrlEncode(granttype)); Jobject.Add('client_id', TypeHelper.UrlEncode(clienid)); Jobject.Add('username', TypeHelper.UrlEncode(username)); Jobject.Add('password', TypeHelper.UrlEncode(password)); Jobject.WriteTo(tmpString); cont.WriteFrom(tmpString); cont.ReadAs(tmpString); cont.GetHeaders(header); header.Add('charset', 'UTF-8'); header.Remove('Content-Type'); header.Add('Content-Type', 'application/x-www-form-urlencoded'); client.Post('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', cont, response); response.Content.ReadAs(ResponseText); end;
Answers
With your code you are sending a JSON format and you need to send something like this:
And every value must be urlEncode like you already do.
Regards.
And in addition to the reply of @ftornero your BC code is similar to a 'raw' body while you should use 'x-www-form-urlencoded'.
Code I used in C/AL:
- Put the key/value pairs in an Outstream
- TempBlob.Blob.CREATEINSTREAM(InStream);
- TempBlob.Blob.CREATEOUTSTREAM(OutStr);
- Copy to Instream
- SetContentType to application/x-www-form-urlencoded (HttpWebRequestMgt.SetContentType('application/x-www-form-urlencoded');)
- Add the stream to the body of your message: HttpWebRequestMgt.AddBodyBlob(TempBlob);
how do we add this headers?
for GET
Change the "application/x-www-form-urlencoded" for "application/json" and add the two others one.
Regards.
this is my code
but this is my return value
it supposed to be like this
in postman i tested is ok
With this code
You don't pass the RequestMsg that you need to pass.
Use
This is the URL for the documentation
https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/httpclient/httpclient-data-type
Regards
I tried this before
got this error message
There is two header, so put the "Content-Type" in the first one and the autorization part in the second one, but if the method is GET I think that the "Content-Type" part is no necesary because you don't send any data in this case.
Regards
I have sent pm to you about my key.
there's another error message
I already sent my answer.
Regards
i am still stuck with this.
any idea or link you can share how to use azure function, and then BC call this azure function?
Try this code
And you must get this message:
This is the Azure Function Code, the Client-ID and the Client-Secret must been the correct ones.
You could elaborate the Azure function to get as parameters the Method, the URL, the X-IBM-Client-ID, etc.
This Azure function will be active for a couple of days.
Regards.
thanks for the effort
I am not really familiar with this azure function.
are we able to pass parameter to this azure function?
such as clientid, clientsecret and url
You must to create the Azure function, and sure you can pass parameters to it.
The AL code to do the new call
And the new Azure function
Regards
This line:
Must be this one:
``ñ
<b> codeunit 80100 "LWA Access Token"
{
trigger OnRun()
begin
end;
procedure Refresh_LWA_Token(): Text
var
client: HttpClient;
cont: HttpContent;
header: HttpHeaders;
response: HttpResponseMessage;
Jobject: JsonObject;
tmpString: Text;
TypeHelper: Codeunit "Type Helper";
Client_ID: Text;
Client_Secret: Text;
grant_type: Text;
refresh_token: Text[10000];
ResponseText: Text;
begin
Client_ID := 'xxxxxxxxxxxxxxxx';
Client_Secret := 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
grant_type := 'refresh_token';
refresh_token := 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
tmpString := 'client_id=' + TypeHelper.UrlEncode(Client_ID) +
'&client_secret=' + TypeHelper.UrlEncode(Client_Secret) +
'&refresh_token=' + TypeHelper.UrlEncode(refresh_token) +
'&grant_type=' + TypeHelper.UrlEncode(grant_type);
cont.WriteFrom(tmpString);
cont.ReadAs(tmpString);
cont.GetHeaders(header);
header.Add('charset', 'UTF-8');
header.Remove('Content-Type');
header.Add('Content-Type', 'application/x-www-form-urlencoded');
client.Post('https://api.amazon.com/auth/o2/token', cont, response);
response.Content.ReadAs(ResponseText);
if (response.IsSuccessStatusCode) then begin
Message(ResponseText);
end
else
Message(ResponseText);
end;
var
myInt: Integer;
} </b>