RestSharp Oauth 2.0

My Forex News app (WP8) has been released. Yeay for that! Currently working on my next app. In the meantime, I am in the process of creating an API wrapper for Pocket/Read It Later for C#. Initially I implemented all the Oauth 2.0 manually using WebClient class, but figured out that was the easy part. As I need to GET data (Json) thru data, then I would need to implement Json serializer/deserializer. My initial thought is to use Newton Json, but then it would make more sense if I just leverage on RestSharp. Then I do not need to implement the Oauth process (although I have coded mine, going to scrap those) and let it manage the serialization/deserialization.

The lack of documentation of RestSharp is really annoying. I couldn’t find anything related to Oauth 2.0 implementation. But after trial and error these below code should work to get the request_token. And the rest of the process should be pretty trivial.

    private const string BaseUri = "https://getpocket.com";
    private const string ConsumerKey = "your-consumer-key-from-Pocket";
 
    private void Login_OnClick(object sender, RoutedEventArgs e)
    {
        var client = new RestClient(BaseUri);
        // Although the API specifies /v3/oauth/request, it gives out 406 Error if no .php extension
        // Request thru Fiddler works fine without the extension
        var request = new RestRequest("/v3/oauth/request.php", Method.POST);
     
        // Option 1: Use the SimpleAuthenticator
        client.Authenticator = new SimpleAuthenticator("consumer_key", ConsumerKey, "redirect_uri", "http://www.google.com");
     
        // Option 2: Use the application/json
        // This is the most flexible option. Mostly useful to POST data to server
        request.AddHeader("Host", "getpocket.com");
        request.AddHeader("Content-Type", "application/json; charset=UTF-8");
        request.AddHeader("X-Accept", "application/json");
        request.RequestFormat = DataFormat.Json;
        var jsonObj = new { consumer_key = ConsumerKey, redirect_uri = "http://www.google.com" };
        request.AddBody(jsonObj);
     
        // Option 3: Use application/x-www-url-encoded
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        request.AddParameter("consumer_key", ConsumerKey);
        request.AddParameter("redirect_uri", "http://www.google.com");
     
        client.ExecuteAsync(request, response => Response.Text = response.ContentType + " " + response.Content);
    }

So just pick and choose whatever option you want to use. I would prefer Option 2 and 3. Option 1 is very simplistic but good enough to do the initial authentication process and the rest should be done by Option 2 and 3.