Consume 3rd Party API in D365 Finops Using X 1685853820
Consume 3rd Party API in D365 Finops Using X 1685853820
By SAIM SIDDIQUI
This document will help you understand how to consume third party api with
Dynamics 365 FinOps using x++
Authorization:
For authorization we have client id and secret value and an authentication URL. We will
use to get token and with this token we will be authorize for every request we sent.
So here I sent request with client id and secret value to the given address and get token
of type bearer now we we can request for any api with this key.
So for getting this token we need to create request class for mapping the request and
response for mapped the output.
Notes: Since here we will pass client Id and secret value so we might not use
request class.
class SS_TokenRequest
{
Name clientId,secretValue;
[DataMemberAttribute("client_id")]
public Name parmClientId(Name _clientId= clientId)
{
clientId = _clientId;
return clientId;
}
[DataMemberAttribute("client_Secret")]
public Name parmSecretValue(Name _secretValue= secretValue)
{
secretValue= _secretValue;
return secretValue;
}
class SS_TokenResponse
{
Name tokenType,token;
[DataMemberAttribute("token_type")]
public Name parmTokenType(Name _clientId= clientId)
{
clientId = _clientId;
return clientId;
}
[DataMemberAttribute("access_token")]
public Name parmAccessToken(Name _secretValue= secretValue)
{
secretValue= _secretValue;
return secretValue;
}
}
Class authenticationHelper
{
try
{
httpHeader = new System.Net.WebHeaderCollection();
new InteropPermission(InteropKind::ClrInterop).assert();
clrObj = System.Net.WebRequest::Create(_url);
request = clrObj;
request.set_Headers(httpHeader);
request.Method = "GET";
request.ContentType = "application/json";
requestStream = request.GetRequestStream();
streamWriter = new System.IO.StreamWriter(request.GetRequestStream());
response = request.GetResponse();
System.IO.StreamReader streamRead = new
System.IO.StreamReader(response.GetResponseStream());
responseToReturn = streamRead.ReadToEnd();
tokenResponse =
FormJsonSerializer::deserializeObject(classNum(SS_TokenResponse),responseToRetu
rn);
token = tokenResponse.parmTokenType() + “ ”+
tokenResponse.parmAccessToken();
}
catch
{
ex = CLRInterop::getLastException().GetBaseException();
error(ex.get_Message());
return token;
}
}
Now you have token so you can now send request in a same way with token on
header.
Request class:
[DataContractAttribute]
class SS_AssetContract
{
HcmPersonnelNumberId employeeCode;
DocumentNum assetKey;
Description255 assetName,fixedAssetNumber;
FromDate fromDate;
ToDate toDate;
boolean decrease;
Amount amount;
Notes notes;
[DataMemberAttribute("EmployeeCode")]
public HcmPersonnelNumberId parmEmployeeCode(HcmPersonnelNumberId
_employeeCode = employeeCode)
{
employeeCode = _employeeCode;
return employeeCode;
}
[DataMemberAttribute("FixedAssetNumber")]
public Name parmFixedAssetNumber(Name _fixedAssetNumber =
fixedAssetNumber)
{
fixedAssetNumber = _fixedAssetNumber;
return fixedAssetNumber;
}
[DataMemberAttribute("AssetKey")]
public DocumentNum parmAssetKey(DocumentNum _assetKey = assetKey)
{
assetKey = _assetKey;
return assetKey;
}
[DataMemberAttribute("AssetName")]
public Description255 parmAssetName(Description255 _assetName = assetName)
{
assetName = _assetName;
return assetName;
}
[DataMemberAttribute("FromDate")]
public FromDate parmFromDate(FromDate _fromDate = fromDate)
{
fromDate = _fromDate;
return fromDate;
}
[DataMemberAttribute("ToDate")]
public ToDate parmToDate(ToDate _toDate = toDate)
{
toDate = _toDate;
return toDate;
}
[DataMemberAttribute("Decrease")]
public boolean parmDecrease(boolean _decrease = decrease)
{
decrease = _decrease;
return decrease;
}
[DataMemberAttribute("Amount")]
public Amount parmAmount(Amount _amount= amount)
{
amount = _amount;
return amount;
}
[DataMemberAttribute("Notes")]
public Notes parmNotes(Notes _notes = notes)
{
notes = _notes;
return notes;
}
}
Sending Request
try
{
httpHeader = new System.Net.WebHeaderCollection();
new InteropPermission(InteropKind::ClrInterop).assert();
clrObj = System.Net.WebRequest::Create(_url);
request = clrObj;
httpHeader.Add("Authorzation",_token);
request.set_Headers(httpHeader);
request.Method = "POST";
request.ContentType = "application/json";
requestStream = request.GetRequestStream();
streamWriter = new System.IO.StreamWriter(request.GetRequestStream());
streamWriter.Write(_request);
streamWriter.Flush();
streamWriter.Close();
response = request.GetResponse();
System.IO.StreamReader streamRead = new
System.IO.StreamReader(response.GetResponseStream());
responseToReturn = streamRead.ReadToEnd();
}
catch
{
ex = CLRInterop::getLastException().GetBaseException();
error(ex.get_Message());
responseToReturn = ex.get_Message();
}
return responseToReturn;