Skip to main content

Notifications

Announcements

No record found.

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();

  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    Calling external webservice through X++ - OAuth 2.0 header
    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.
  • bankk Profile Picture
    bankk 6,603 on at
    Calling external webservice through X++ - OAuth 2.0 header
    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans