Geeks With Blogs
Mohamed Haamdi All about Mobile

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. Posted on Monday, June 11, 2012 10:57 PM Windows Phone | Back to top


Comments on this post: OAuth with RestSharp in Windows Phone

# re: OAuth with RestSharp in Windows Phone
Requesting Gravatar...
hm great info. after that we should readfunny text messages
Left by emma on Jun 13, 2012 12:56 AM

# re: OAuth with RestSharp in Windows Phone
Requesting Gravatar...
Great article, Thanks!
Left by Soroush Mirzaei on Jun 25, 2012 3:59 PM

# re: OAuth with RestSharp in Windows Phone
Requesting Gravatar...
i like it a lot
Left by Nawel on Jun 26, 2012 2:22 PM

# re: OAuth with RestSharp in Windows Phone
Requesting Gravatar...
I found this rather useful
Left by DevGeek on Jun 26, 2012 5:36 PM

# re: OAuth with RestSharp in Windows Phone
Requesting Gravatar...
I would use the PIN-Based OAuth Method if it was me
Left by Sam on Jun 26, 2012 5:38 PM

# re: OAuth with RestSharp in Windows Phone
Requesting Gravatar...
Great article i found it very helpful
Left by eric on Jun 26, 2012 6:08 PM

# re: OAuth with RestSharp in Windows Phone
Requesting Gravatar...
You can actually do this without RestSharp... I made a public Foursquare App called 4SQ (4sq.codeproject.com), we show you how to easily use the browser control to get the OAuth code...

If you're interested also a video on TechMeShow on youtube.

+ Great content buddy!

Thanks,
Lance
Left by Lance Seidman on Aug 03, 2012 7:06 AM

Your comment:
 (will show your gravatar)


Copyright © midoBB | Powered by: GeeksWithBlogs.net