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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Calling external webservice through X++ - OAuth 2.0 header

(0) ShareShare
ReportReport
Posted on by 150

Hi All,

I'm calling a web service through X++, but it's giving me 401 Unauthorized because of OAuth 2.0, can anyone please help me on adding the header authorization to my request. here is my code, note that i can make c# project and add it as a reference but im looking to have it here through X++ instead.

System.Net.HttpWebRequest webRequest;
System.IO.Stream stream;
System.Net.ServicePoint servicePt;
System.Text.Encoding utf8;
System.Byte[] bytes;
System.String sysString;
System.IO.StreamReader streamReader = null;

//For OAuth
str aadTenant;
str aadClientAppId;
str aadClientAppSecret;
str aadResource;


utf8 = System.Text.Encoding::get_UTF8();
bytes = utf8.GetBytes(jsonRequest);

clrObj = System.Net.WebRequest::Create(url);

webRequest = clrObj;
webRequest.set_Method('POST');
webRequest.set_KeepAlive(true);
webRequest.set_ContentType('application/json');
webRequest.set_ContentLength(bytes.get_Length());

servicePt = webRequest.get_ServicePoint();
servicePt.set_Expect100Continue(false);


stream = webRequest.GetRequestStream();
stream.Write(bytes,0,bytes.get_Length());
stream.Close();

I have the same question (0)
  • bankk Profile Picture
    6,609 on at
    Hi,
    Did you end up solving this issue through X++? I'm in the same boat and trying to figure out how to handle it. Thanks.
  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at
    It doesn't matter whether you use C# or X++, because you use exactly the same classes. Classes like System.Net.HttpWebRequest are not specifying to AX, they're a part of .NET Framework. There a good idea is looking for a solution in C# (because you'll find many more resources than about X++) and then rewrite the code to X++ (or just call a C# library from X++ via .NET Interop).
     
    For example, you could borrow OAuthHelper class from F&O integration samples.
     
    Of course, you must already have an app registration in Azure AD (Entra).
     
    Note that capabilities of .NET Interop differs in AX versions, therefore mentioning your version may be important.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 451 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 428 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 278 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans