Posts Tagged REST
LinkedIn OAuth using Hammock in C# & ASP.NET
Posted by suddenelfilio in .net, Web & Design, Web Services on 24/08/2010
UPDATE: Hammock moved to github https://github.com/danielcrenna/hammock
Codeplex.com description of Hammock:
Hammock is a REST library for .NET that greatly simplifies consuming and wrapping RESTful services.

I’m currently working on a little web project that needed integration with LinkedIn.com. The LinkedIn API allows for OAuth authorization and authentication. They describe the process of getting a request token, authorizing it by the user and then getting an access token. Standard OAuth you might say. So I needed a way to do this in C# and found the Hammock library. Although Hammock does not only do OAuth I used it for that purpose only at the moment.
Below is the code I wrote to get a request token and send the user to the authorization page at Linkedin:
public void RequestAndAuthorize()
{
var credentials = new OAuthCredentials
{
CallbackUrl = "http://127.0.0.1/oauth/callback/",
ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"],
Verifier = "123456",
Type = OAuthType.RequestToken
};
var client = new RestClient { Authority = "https://api.linkedin.com/uas/oauth", Credentials = credentials };
var request = new RestRequest { Path = "requestToken" };
RestResponse response = client.Request(request);
string token = response.Content.Split('&').Where(s => s.StartsWith("oauth_token=")).Single().Split('=')[1];
string token_secret = response.Content.Split('&').Where(s => s.StartsWith("oauth_token_secret=")).Single().Split('=')[1];
Response.Redirect("https://api.linkedin.com/uas/oauth?oauth_token=" + token);
}
Once the user has authorized your request token the LinkedIn server will redirect the user back to the callback url in this case this is http://127.0.0.1/oauth/callback. Using the returned oauth_token and oauth_token_secret (you got this one while requesting a request token) you can now get an access token so you can start making authenticated API calls from you application on behalf of the user.
This is the code that is used when the callback url is called:
public void Callback()
{
var credentials = new OAuthCredentials
{
ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"],
Token = token,
TokenSecret = token_secret,
Verifier = verifier,
Type = OAuthType.AccessToken,
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
SignatureMethod = OAuthSignatureMethod.HmacSha1,
Version = "1.0"
};
var client = new RestClient { Authority = "https://api.linkedin.com/uas/oauth", Credentials = credentials, Method = WebMethod.Post };
var request = new RestRequest { Path = "accessToken" };
RestResponse response = client.Request(request);
string content = response.Content;
}
As you can see Hammock is really nice and allows for easy OAuth authentication/authorization to be used.
MS Tag REST interface updated
Posted by suddenelfilio in MSTagLib, Web Services on 30/07/2010
Today I’ve updated the Microsoft Tag REST interface over at http://tag.ws.suddenelfilio.net/.
Since Microsoft released Tag I was a bit behind to update the interface to include the GetTagID functionality. This is now made up for and the service available for all to enjoy.
I also updated the documentation on how to use the REST interface to get a Tag’s id.
If you experience any problems please let me know using the address that you can find over at http://tag.ws.suddenelfilio.net/ or leave a comment on this page!

