From d81a35ef4808b9b390325007c8de53bf26b76cde Mon Sep 17 00:00:00 2001 From: ceee Date: Fri, 21 Jun 2013 00:07:05 +0200 Subject: [PATCH] add first version of authentification; --- PocketSharp.Console/Program.cs | 10 +++--- PocketSharp/Components/Authentification.cs | 40 ++++++++++++++++++++-- PocketSharp/PocketClient.cs | 16 ++++++--- PocketSharp/PocketSharp.csproj | 4 +-- 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/PocketSharp.Console/Program.cs b/PocketSharp.Console/Program.cs index 50da651..3fde9ea 100644 --- a/PocketSharp.Console/Program.cs +++ b/PocketSharp.Console/Program.cs @@ -16,15 +16,13 @@ namespace PocketSharp.Console // this consumerKey is just for demonstration purposes // please create your own application and retrieve it's key. It's a 1-step process ;-) PocketClient client = new PocketClient( - consumerKey: "15396-f6f92101d72c8e270a6c9bb3", - accessCode: "a85134a7-243c-6656-ab82-97c901" + consumerKey: "15396-f6f92101d72c8e270a6c9bb3" ); - client.Search("css").ForEach(delegate(PocketItem item) - { - System.Console.WriteLine(item.FullTitle); - }); + var x = client.GetAuthentificationUri(new Uri("http://ceecore.com")); + System.Console.WriteLine(x.ToString()); + System.Console.ReadKey(); } } diff --git a/PocketSharp/Components/Authentification.cs b/PocketSharp/Components/Authentification.cs index be5b202..eb2f79d 100644 --- a/PocketSharp/Components/Authentification.cs +++ b/PocketSharp/Components/Authentification.cs @@ -1,12 +1,48 @@ -using System; +using PocketSharp.Models.Authentification; +using System; using System.Collections.Generic; using System.Linq; using System.Text; +using RestSharp; namespace PocketSharp { public partial class PocketClient { - + /// + /// Gets the authentification URI. + /// + /// The callback URI. + /// + public Uri GetAuthentificationUri(Uri callbackUri) + { + RequestCode response = GetResource("oauth/request", new List() + { + new Parameter() { Name = "redirect_uri", Value = callbackUri, Type = ParameterType.GetOrPost } + }); + + // save code to client + RequestCode = response.Code; + + return new Uri(string.Format(authentificationUrl, RequestCode, Uri.EscapeDataString(callbackUri.ToString()))); + } + + + /// + /// Authenticates the user + /// + /// + public string Authenticate() + { + AccessCode response = GetResource("oauth/authorize", new List() + { + new Parameter() { Name = "code", Value = RequestCode, Type = ParameterType.GetOrPost } + }); + + // save code to client + AccessCode = response.Code; + + return AccessCode; + } } } diff --git a/PocketSharp/PocketClient.cs b/PocketSharp/PocketClient.cs index 6c86be2..bdc2083 100644 --- a/PocketSharp/PocketClient.cs +++ b/PocketSharp/PocketClient.cs @@ -17,6 +17,11 @@ namespace PocketSharp /// protected static Uri defaultUrl = new Uri("https://getpocket.com/v3/"); + /// + /// The authentification URL + /// + protected static string authentificationUrl = "https://getpocket.com/auth/authorize?request_token={0}&redirect_uri={1}"; + /// /// base URL for the API /// @@ -128,7 +133,7 @@ namespace PocketSharp /// Additional POST parameters /// /// No access token available. Use authentification first. - protected T GetResource(string method, List parameters) where T : class, new() + protected T GetResource(string method, List parameters = null) where T : class, new() { // every single Pocket API endpoint requires HTTP POST data var request = new RestRequest(method, Method.POST); @@ -140,10 +145,13 @@ namespace PocketSharp } // enumeration for params - parameters.ForEach(delegate(Parameter param) + if (parameters != null) { - request.AddParameter(param); - }); + parameters.ForEach(delegate(Parameter param) + { + request.AddParameter(param); + }); + } // do the request return Request(request); diff --git a/PocketSharp/PocketSharp.csproj b/PocketSharp/PocketSharp.csproj index d945b64..1f78d05 100644 --- a/PocketSharp/PocketSharp.csproj +++ b/PocketSharp/PocketSharp.csproj @@ -60,9 +60,7 @@ - - - +