web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

The remote server returned an error: (401) Unauthorized

(0) ShareShare
ReportReport
Posted on by 10

When I call the api to send the invoice without a program middleware .

But when I send the invoice through middleware , it is sent because all the security data is inside this program

This My Code

str destinationUrl = 'api.invoicing.com/.../submission' requestXml, responseXml;
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
CLRObject clrObj;
System.Byte[] bytes;
System.Text.Encoding utf8;
System.IO.Stream requestStream, responseStream;
System.IO.StreamReader streamReader;
System.Exception ex;
System.Net.WebHeaderCollection httpHeader;
str byteStr;
System.Byte[] byteArray;
System.IO.Stream stream;
System.IO.Stream dataStream;
System.Net.NetworkCredential credential;

str token = "xxxxxx" ;   // password token
str clientID = "xxxxxxxxxx"; // clientID

str PrimarySecret = "xxxxxxxxxxx"; // clientSecret 1
str SecondSecret = "xxxxxxxxxxx"; // clientSecret 2

try
{

System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Tls12);
new InteropPermission(InteropKind::ClrInterop).assert();
httpHeader = new System.Net.WebHeaderCollection();
clrObj = System.Net.WebRequest::Create(destinationUrl);
request = clrObj;
utf8 = System.Text.Encoding::get_UTF8();
bytes = utf8.GetBytes(requestXml);
request.set_KeepAlive(true);
request.set_ContentType("application/xml");
utf8 = System.Text.Encoding::get_UTF8();
// byteArray = utf8.GetBytes(clientID+":"+PrimarySecret);
// byteStr = System.Convert::ToBase64String(byteArray);


httpHeader.Add("client_id"+":"+ clientID);
httpHeader.Add("client_secret" +":"+ PrimarySecret);

// httpHeader.Add("client_secret" +":"+ SecondSecret);

httpHeader.Add("Authorization",'Basic' + token);



request.set_UseDefaultCredentials(true);
request.set_PreAuthenticate(true);

request.set_ContentType("application/xml; encoding='utf-8'");
request.set_ContentLength(bytes.get_Length());
request.set_Method("POST");
request.set_Accept("Accept");
request.set_Timeout(3600);
request.set_Headers(httpHeader);
requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.get_Length());
response = request.GetResponse();
responseStream = response.GetResponseStream();
streamReader = new System.IO.StreamReader(responseStream);
responseXml = streamReader.ReadToEnd();
info(responseXml);
}
catch (Exception::CLRError)
{
//bp deviation documented
ex = CLRInterop::getLastException().GetBaseException();
error(ex.get_Message());
}
requestStream.Close();
streamReader.Close();
responseStream.Close();
response.Close();

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    So you're calling some external service and the call fails? I suggest to discuss with the owner of that service - the issue doesn't really seem to be about AX. The owner of that service should be able to provide help, such as documentation about the service.

    I don't understand the description in the beginning of your question, though. Do you already have some way to call the service succesfully? What differences do you see in the succesful vs failing request? 

  • Gadosky Profile Picture
    10 on at

    If i call the service directly, it will not work

    But if i connect through the intermediary services like (Middleware API ), it will be successful because the service provider’s data is inside the intermediary service, such as (customer ID - secret key, password code)

  • Suggested answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    Hi Gadosky,

    thanks for the clarification.

    Please let us know what differences you find out when you compare the requests from the intermediary service and from AX.

  • Gadosky Profile Picture
    10 on at

    Proxy servers installed on localhost include client ID, secret key 1, secret key 2, and password token when invoice is successfully sent by the broker

    But if you send it directly without a moderator as shown in the above code, the error will be answered (

    The remote server returned an error: (401) Unauthorized)

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 671

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 621 Super User 2026 Season 1

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 589 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans