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)