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

Announcements

No record found.

News and Announcements icon
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();

  • bankk Profile Picture
    6,611 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
    241,349 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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

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

#1
Martin Dráb Profile Picture

Martin Dráb 511 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 426 Super User 2026 Season 2

#3
CU-1234529-002 Profile Picture

CU-1234529-002 159

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans