Over a million developers have joined DZone.

OAuth With RestSharp in Windows Phone

· Mobile Zone

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.

Topics:

Published at DZone with permission of Mohamed Haamdi. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}