OAuth with RestSharp in Windows Phone

Posted by midoBB on Geeks with Blogs See other posts from Geeks with Blogs or by midoBB
Published on Mon, 11 Jun 2012 20:57:04 GMT Indexed on 2012/06/11 22:41 UTC
Read the original article Hit count: 229

Filed under:

Nearly every major API provider uses OAuth for the user authentication and while it is easy to understand the concept, using it in a Windows Phone app isn’t pretty straightforward. So for this quick tutorial we will be using RestSharp for WP7 and the API from getglue.com (an entertainment site) to authorize the user.

So the first step is to get the OAuth request token and then we redirect our browserControl to the authorization URL

private void StartLogin()
        {
 
            var client = new RestClient("https://api.getglue.com/");
            client.Authenticator = OAuth1Authenticator.ForRequestToken("ConsumerKey", "ConsumerSecret");
            var request = new RestRequest("oauth/request_token");
            client.ExecuteAsync(request, response =>
            {
                _oAuthToken = GetQueryParameter(response.Content, "oauth_token");
                _oAuthTokenSecret = GetQueryParameter(response.Content, "oauth_token_secret");
                string authorizeUrl = "http://getglue.com/oauth/authorize" + "?oauth_token=" + _oAuthToken + "&style=mobile";
                Dispatcher.BeginInvoke(() =>
                {
                    browserControl.Navigate(new Uri(authorizeUrl));
                }); 
               
            });
        }
        
        private static string GetQueryParameter(string input, string parameterName)
        {
            foreach (string item in input.Split('&'))
            {
                var parts = item.Split('=');
                if (parts[0] == parameterName)
                {
                    return parts[1];
                }
            }
            return String.Empty;
        }

Then we listen to the browser’s Navigating Event

private void Navigating(Microsoft.Phone.Controls.NavigatingEventArgs e)
        {
            if (e.Uri.AbsoluteUri.Contains("oauth_callback"))
            {
                var arguments = e.Uri.AbsoluteUri.Split('?');
                if (arguments.Length < 1)
                    return;
                GetAccessToken(arguments[1]);
            }
        }
private void GetAccessToken(string uri)
        {
            var requestToken = GetQueryParameter(uri, "oauth_token");
            var client = new RestClient("https://api.getglue.com/");
            client.Authenticator = OAuth1Authenticator.ForAccessToken(ConsumerKey, ConsumerSecret, _oAuthToken, _oAuthTokenSecret);
            var request = new RestRequest("oauth/access_token");
            client.ExecuteAsync(request, response => {
                AccessToken = GetQueryParameter(response.Content, "oauth_token");
                AccessTokenSecret = GetQueryParameter(response.Content, "oauth_token_secret");
                UserId = GetQueryParameter(response.Content, "glue_userId");
                
            });
        }

Now to test it we can access the user’s friends list

var client = new RestClient("http://api.getglue.com/v2");
            client.Authenticator = OAuth1Authenticator.ForProtectedResource(ConsumerKey, ConsumerSecret, GAccessToken, AccessTokenSecret);
            var request = new RestRequest("/user/friends");
            request.AddParameter("userId", UserId,ParameterType.GetOrPost);
           // request.AddParameter("category", "all",ParameterType.GetOrPost);
            client.ExecuteAsync(request, response =>
            {
               TreatFreindsList();
            });
And that’s it now we can access all OAuth methods using RestSharp.

© Geeks with Blogs or respective owner