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 :
Microsoft Dynamics AX (Archived)

Send JSON data to external WebService from AX2009 using bearer token Authentication

(0) ShareShare
ReportReport
Posted on by 62

Hi everyone,

key words here are Bearer token Authentication (not basic authentication).

Did anyone already consume external webservice from AX2009 using bearer token auth ?

following code won't work and I still get either "The remote server returned an error: (415) Unsupported Media Type." or

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


    System.Net.HttpWebRequest       request;
    System.Net.HttpWebResponse      response;
    System.Byte[]                   byteArray, byteArrayToken;
    System.IO.Stream                dataStream;
    System.IO.StreamReader          streamReader;
    System.Net.ServicePoint         servicePoint;
    System.Net.ServicePointManager  servicePointManager;
    System.Net.WebHeaderCollection  httpHeader;
    CLRObject                       clrObj;
    System.Text.Encoding            utf8;
    str                             bodyData;
    str                             token = 'xxxxxxxxxxx';
    XYZParameters                   xyzParameters = XYZParameters::find();
    ;
    
    bodyData = "{'xxxx': 'yyyy'}"
    
    new InteropPermission(InteropKind::ClrInterop).assert();
    clrObj = System.Net.WebRequest::Create(xyzParameters.URL);
    System.Net.ServicePointManager::set_Expect100Continue(false);
    
    httpHeader = new System.Net.WebHeaderCollection();
    
    request = clrObj;
    request.set_Method("POST");
    request.set_ContentType("application/json");
    
    utf8 = System.Text.Encoding::get_UTF8();
    
    byteArray = utf8.GetBytes(bodyData);
    request.set_ContentLength(byteArray.get_Length());
    
    // first possibility : passing clear token
    // won't work. CLR Error on GetRequestStream line below : "The remote server returned an error: (415) Unsupported Media Type."
    httpHeader.Add("Authorization", strfmt("bearer %1", token));
    
    // second possibility : passing encoded token
    // won't work. CLR Error on GetRequestStream line below: "The remote server returned an error: (401) Unauthorized." though token is correct and    authorized when tested elsewhere
    byteArrayToken = utf8.GetBytes(token);
    byteTokenStr = System.Convert::ToBase64String(byteArrayToken);
    httpHeader.Add("Authorization", strfmt("bearer %1", byteTokenStr));
    
    request.set_Headers(httpHeader);
        
    dataStream = request.GetRequestStream(); // line where CLR error is thrown
    dataStream.Write(byteArray, 0, byteArray.get_Length());
    dataStream.Close();

*This post is locked for comments

I have the same question (0)
  • Soren Andersen Profile Picture
    530 on at

    Are you sure you got a valid token? If yes - I am not sure if it is case sensitive - but in my running solution the bearer is with capital b - Bearer

    If the body-data is incorrect - your request might be rejected too.

  • Nathanael.tetouom Profile Picture
    62 on at

    Thanks for your reply. My token is valid as well as body-data (using postman tool, i'm able to consume web service with that same parameters). Bearer with capital b didn't work either. Are you converting token to base 64 string in your running solution ?

  • Soren Andersen Profile Picture
    530 on at

    No - it is just passed directly: www.screencast.com/.../4MGksaWh

     - I get my token from another service call. Just to eliminate all possible error - you cannot reuse the token from postman in your calls from Ax.

  • Verified answer
    Nathanael.tetouom Profile Picture
    62 on at

    I finally found what was wrong :

    In my above code, HttpHeader needed to be set before request (method and contentType)

  • Community Member Profile Picture
    on at

    Hi. can you please post the final code you used..

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans