API
Version 7.x > API > 401.1 Unauthroized Rest API View modes: 
User avatar
Certified Developer v7
Certified  Developer v7
Joe - 9/10/2013 4:05:33 PM
   
401.1 Unauthroized Rest API
Hi,

I am trying to authenticate to my Rest API. Here is my site:

http://www.jmawebtechnologies.com/rest/ecommerce.sku/all

I am using my administrator user name and password, but I get a 401.1 authorized error. What user name and password do I use? I have basic as the authentication setting in the Kentico admin. Here is my code:

        public List<COM_SKU> GetAllSKUS()
{
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(m_UserName, m_Password);

HttpClient client = new HttpClient(handler)
{
BaseAddress = new Uri(m_BaseUrl),
};

string url = String.Format("/rest/ecommerce.sku/all");


HttpResponseMessage response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
List<COM_SKU> result = response.Content.ReadAsAsync<List<COM_SKU>>().Result;
return result;
}

return null;


}

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 9/11/2013 7:37:16 AM
   
RE:401.1 Unauthroized Rest API
Did you enable the REST service in CMSSiteManager>Settings>Integration>REST? Also what's your server environment? There are a few IIS features which have to be installed. Check out my post here.

User avatar
Certified Developer v7
Certified  Developer v7
Joe - 9/11/2013 10:39:57 AM
   
RE:401.1 Unauthroized Rest API
Hi,

It's an issue with the HttpClient. Here is the code works:
        public COM_Order GetOrderById(string orderId)
{
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(m_UserName, m_Password);

HttpClient client = new HttpClient(handler)
{
};

string url = m_BaseUrl + String.Format("/rest/ecommerce.order/{0}?format=json", orderId);
HttpResponseMessage response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
data result = response.Content.ReadAsAsync<data>().Result;
//result is null, even though valid JSON is returned
return result.Items.FirstOrDefault();
}

return null;

}

Here is my class:

http://pastebin.com/kUca0whm

I used basic as my authentication setting. My user name and password is the administrator account for Kentico. I am unable to serialize the results. What am I doing incorrectly?

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 9/12/2013 8:11:08 AM
   
RE:401.1 Unauthroized Rest API
I'd suggest downloading the REST Testing interface Karol Jarkovsky created. Also check out Brian McKeiver's blog post about REST.

User avatar
Certified Developer v7
Certified  Developer v7
Joe - 9/12/2013 8:35:36 AM
   
RE:401.1 Unauthroized Rest API
Hi,

I got this code to work:

        public COM_Order GetOrderById(string orderId)
{
data result = new data();
string url = m_BaseUrl + String.Format("/rest/ecommerce.order/{0}", orderId);
WebRequest httpRequest = WebRequest.Create(url);
string userP = m_UserName + ":" + m_Password;
byte[] authBytes = Encoding.UTF8.GetBytes(userP).ToArray();
httpRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(authBytes));
WebResponse httpResponse = httpRequest.GetResponse();
Stream responseStream = httpResponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string resultData = reader.ReadToEnd();
XmlSerializer _oxs = new XmlSerializer(typeof(data));
data _Response = new data();
StringReader _InXml = new StringReader(resultData);
result = (data)_oxs.Deserialize(_InXml);
responseStream.Close();
httpResponse.Close();

if (result.Orders.Count() > 0)
{
return result.Orders[0];
}

return null;

}