0% found this document useful (0 votes)
238 views9 pages

Consume 3rd Party API in D365 Finops Using X 1685853820

The document discusses consuming a 3rd party API from Dynamics 365 Finance and Operations using X++. It covers getting an authorization token, creating request and response classes to map data, and sending a request to the API by passing the token in the header.

Uploaded by

Dhruv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views9 pages

Consume 3rd Party API in D365 Finops Using X 1685853820

The document discusses consuming a 3rd party API from Dynamics 365 Finance and Operations using X++. It covers getting an authorization token, creating request and response classes to map data, and sending a request to the API by passing the token in the header.

Uploaded by

Dhruv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Consume 3rd party API in

Dynamics 365 FinOps


using x++

By SAIM SIDDIQUI
This document will help you understand how to consume third party api with
Dynamics 365 FinOps using x++

So there is a case where we need to transfer asset to other worker in D365FinOps so


for this we also need to push that details to other system for this reason we will
consume 3rd party API.

So we have two steps:


1. Authorization
2. Sending Request.

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
{

Public notes getToken(SS_TokenRequest _tokenRequest,Url _url)


{
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
CLRObject clrObj;
System.Exception ex;
System.Net.WebHeaderCollection httpHeader;
System.IO.Stream requestStream, responseStream;
System.IO.StreamWriter streamWriter;
SS_TokenResponse tokenResponse;
Notes token;

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

Public void sendDataToThirdParty(Notes _token,Url _url,SS_AssetContract


_request)
{
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
CLRObject clrObj;
System.Exception ex;
System.Net.WebHeaderCollection httpHeader;
System.IO.Stream requestStream, responseStream;
System.IO.StreamWriter streamWriter;

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;

You might also like