Tuesday, August 21, 2012

TradeShift REST API Explorer with OAuth Lite

While I've been exploring TradeShift's API I actually fell flat when I needed to do actual getting/posting stuff with technology I am most familiar with: Microsoft .NET Framework. I am sure there are lot's of folks using this technology that would like to benefit from TadeShift doing things: be able to integrate with this awesome network.
Unfortunately I couldn't find any comprehensible/maintainable approach to use OAuth authentication with .NET so I had to do one myself like it happens often enough with some new cool stuff emerging out there. I've shared what I've done through CodePlex and you can feel free to download the source code.

The library is extremely easy to use. It provides an OAuthClient class derived from WebClient and you can use in the same manner you use WebClient.


var oauthSecret = new OAuthSecret ("ConsumerKey", "ConsumerSecret", "Token", "TokenSecret"); 
var oauth = new OAuthClient(oauthSecret); 
oauth.Headers["Accept"] = "text/xml";
oauth.Headers["Accept-Encoding"] = "utf-8";
oauth.Headers["Content-Type"] = "text/xml";
oauth.Headers["User-Agent"] = "DOTNET API Explorer/1.0";
oauth.Headers["X-Tradeshift-TenantId"] = "Your Tradeshift Tenant Id";

//use this method to GET what you want from TradeShift
var data = oauth.DownloadData("https://api-sandbox.tradeshift.com/tradeshift/rest/external/network/connections");


//use this method to upload content to TradeShift
var upload = new byte [0]; 
data = oauth.UploadData("", "PUT", upload);

Now you have two methods to manipulate your web resources. To GET you need to use DownloadData method and for everything else you can use UploadData.
To find out what are your values for consumer key, consumer secret, token and token secret you need to install API Access to own Account application.

I've also developed a simple TradeShift API Explorer application which is very similar to the Java application     on TradeShift site ( check Exploring the Tradeshift REST API article).  You can install .NET version of API Explorer here.

Happy coding!

No comments:

Post a Comment