Hello,
I must connect to API via Navision 2015. For hurry test I make DLL Library in MSVisual Studio 2017 with NetFrameWork 4.5 . That is code:
using System.IO;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json.Linq;
using System.Text;
namespace TAndT
{
public class TrackAndTrace
{
public string UserName { get; set; }
public string SecretKey { get; set; }
public string Link { get; set; }
public string Token { get; set; }
public string RequestResult { get; private set; }
public bool StatusOperation { get; private set; }
public System.Net.HttpStatusCode StatusCodeOperation { get; private set; }
public string StatusReasonPhrase { get; private set; }
public string RequestLink { get; set; }
public string FilePathRequest { private get; set; }
public TrackAndTrace(string mUserName, string mSecretKey, string mLink)
{
UserName = mUserName;
SecretKey = mSecretKey;
Link = mLink;
}
private void ClearStatus()
{
StatusOperation = false;
StatusReasonPhrase = "";
StatusCodeOperation = System.Net.HttpStatusCode.Unused;
}
private void SetStatus(bool pStatusOperation,string pReasonPhrase,System.Net.HttpStatusCode pStatusCode)
{
StatusOperation = pStatusOperation;
StatusReasonPhrase = pReasonPhrase;
StatusCodeOperation = pStatusCode;
}
public void GetTokenAsync()
{
ClearStatus();
HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string> {
{ "client_id", UserName },
{ "client_secret", SecretKey },
{ "grant_type", "client_credentials" } });
var client = new HttpClient();
var tokenresponse = client.PostAsync(Link, content).Result;
var tokenResponseAsString = tokenresponse.Content.ReadAsStringAsync().Result;
Token = JObject.Parse(tokenResponseAsString)["access_token"].Value<string>();
SetStatus(tokenresponse.IsSuccessStatusCode,tokenresponse.ReasonPhrase,tokenresponse.StatusCode);
}
public void SendRequest()
{
ClearStatus();
var client = new HttpClient();
var requestPost = new HttpRequestMessage(HttpMethod.Post, RequestLink);
requestPost.Headers.Accept.Clear();
requestPost.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
requestPost.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
string xmlPost = File.ReadAllText(FilePathRequest);
requestPost.Content = new StringContent(xmlPost, Encoding.UTF8, "application/xml");
HttpResponseMessage responseInsert = client.SendAsync(requestPost).Result;
RequestResult = responseInsert.Content.ReadAsStringAsync().Result;
SetStatus(responseInsert.IsSuccessStatusCode, responseInsert.ReasonPhrase, responseInsert.StatusCode);
}
}
}
I compiled this library and set into navision server server folder to use it.
In navision create codeunit with code:
TT:=TT.TrackAndTrace('','','');
TT.UserName:='...........';
TT.SecretKey:='............';
TT.Link:='
https://auth-qlf.caas.tpd-pmp.as8677.net/auth/realms/jti_qualif_corporate/protocol/openid-connect/token';
TT.FilePathRequest:='c:/temp/rec.xml';
TT.RequestLink:='
https://cm.qlf-jti-corporate.tpd.as8677.net/v1/cm/corporate/epcis';
TT.GetTokenAsync;
TokenString:=TT.Token();
MESSAGE(TokenString);
The variable TT is as Track And Trace instance of DotNet with DLL librirary.
When I execute this code from codeunit I recevive error with text "Could not create SSL/TLS secure channel" from the component.
I try to execute in Navision 2015,2016,2017. In 2018 and Business Central code is executed succesfully.
Where I make mistake?
Regards
Answers
https://cloudblogs.microsoft.com/dynamics365/it/2018/12/06/how-to-get-earlier-versions-of-the-dynamics-nav-development-environment-to-work-with-tls-1-2/?source=nav