Sunny  Sharma

Sunny Sharma

  • 117
  • 16.1k
  • 1.8m

How to use Magento REST API from C#

Jul 18 2012 8:11 AM

I'm looking for samples on how to use magento rest api from c#. I did googling for a long time but could not find any single sample. All I found was a php one which I could not figure out except a little.

Using a dropbox oauth sample I found on the net I tried to make it work for magento :

private void button1_Click(object sender, RoutedEventArgs e) 

var consumerKey = "xxxxxxxxxxxxx"; 

var consumerSecret = "xxxxxxxxxxxxxxxx";


var uri = new Uri("http://www.MagentoWebsite.com/oauth/token");


// Generate a signature 

OAuthBase oAuth = new OAuthBase(); 

string nonce = oAuth.GenerateNonce(); 

string timeStamp = oAuth.GenerateTimeStamp(); 

string parameters; 

string normalizedUrl; 

string signature = oAuth.GenerateSignature(uri, consumerKey, consumerSecret, 

String.Empty, String.Empty, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1, 

out normalizedUrl, out parameters);


signature = HttpUtility.UrlEncode(signature);


StringBuilder requestUri = new StringBuilder(uri.ToString()); 

requestUri.AppendFormat("?oauth_consumer_key={0}&", consumerKey); 

requestUri.AppendFormat("oauth_nonce={0}&", nonce); 

requestUri.AppendFormat("oauth_timestamp={0}&", timeStamp); 

requestUri.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1"); 

requestUri.AppendFormat("oauth_version={0}&", "1.0"); 

requestUri.AppendFormat("oauth_signature={0}", signature);


var request = (HttpWebRequest)WebRequest.Create(new Uri(requestUri.ToString())); 

request.Method = WebRequestMethods.Http.Get;


var response = request.GetResponse();


var queryString = new StreamReader(response.GetResponseStream()).ReadToEnd();


var parts = queryString.Split('&'); 

var token = parts[1].Substring(parts[1].IndexOf('=') + 1); 

var tokenSecret = parts[0].Substring(parts[0].IndexOf('=') + 1);


queryString = String.Format("oauth_token={0}", token); 

var authorizeUrl = "http://www.MagentoWebsite.com/admin/oauth_authorize?"+queryString; 

Process.Start(authorizeUrl); 

}

Unfortunately this returns a as BAD REQUEST. If anybody is aware of, Please help.

Any help is appreciated.

Best Regards,

Sunny_K