by Arra Derderian
4. May 2011 02:53
Looking to get O-Auth working for my Windows Phone 7 application and I started using the Hammock RESTful helper library. It works great for O-Auth because it handles all the messy stuff behind the scenes. You can literally set the appropriate parameters in your methods and Hammock does the rest. Here is a code snapshot:
RestClient client = new RestClient()
{
Authority = "https://query.yahooapis.com/v1",
Credentials = new OAuthCredentials()
{
ConsumerKey = key,
ConsumerSecret = secret,
Token = oauth_token,
TokenSecret = oauth_token_secret,
SignatureMethod = OAuthSignatureMethod.HmacSha1,
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
Type = OAuthType.ProtectedResource,
Version = "1.0"
}
};
var teamRequest = new RestRequest
{
Path = "/yql?q=select*fromfantasysports.leagues"
};
RestResponse rr = client.Request(teamRequest);
This lets me query my Yahoo! fantasy sports league and return data about my teams in the league. Unfourtunately, Yahoo! has been less than helpful with some of the O-Auth problems I have been having with them. My token keeps getting rejected and their support is terrible. I am waiting on them to get this piece working but in the meantime I have been able to connect my Windows Phone 7 app to my WCF service and use Hammock to make all my asynchronous requests. Because Windows Phone 7 only supports asynchronous requests from your application Hammock is a nice library to properly format and contain all your requests.
One drawback is if you do not use the WebClient class for your web requests then you will have to use the Dispatcher object when updating the UI of your phone app. The WebClient class takes care of this for you but using your own RESTful helper then you need to use Dispatcher in your callback. Here is a code example:
this.Dispatcher.BeginInvoke(() =>
{
//YOUR UI CODE
});