Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

OAuth With RestSharp in Windows Phone

DZone's Guide to

OAuth With RestSharp in Windows Phone

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

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.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}