add first version of authentification;

This commit is contained in:
2013-06-21 00:07:05 +02:00
parent 1f5c4d672c
commit d81a35ef48
4 changed files with 55 additions and 15 deletions
+4 -6
View File
@@ -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();
}
}
+38 -2
View File
@@ -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
{
/// <summary>
/// Gets the authentification URI.
/// </summary>
/// <param name="callbackUri">The callback URI.</param>
/// <returns></returns>
public Uri GetAuthentificationUri(Uri callbackUri)
{
RequestCode response = GetResource<RequestCode>("oauth/request", new List<Parameter>()
{
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())));
}
/// <summary>
/// Authenticates the user
/// </summary>
/// <returns></returns>
public string Authenticate()
{
AccessCode response = GetResource<AccessCode>("oauth/authorize", new List<Parameter>()
{
new Parameter() { Name = "code", Value = RequestCode, Type = ParameterType.GetOrPost }
});
// save code to client
AccessCode = response.Code;
return AccessCode;
}
}
}
+12 -4
View File
@@ -17,6 +17,11 @@ namespace PocketSharp
/// </summary>
protected static Uri defaultUrl = new Uri("https://getpocket.com/v3/");
/// <summary>
/// The authentification URL
/// </summary>
protected static string authentificationUrl = "https://getpocket.com/auth/authorize?request_token={0}&redirect_uri={1}";
/// <summary>
/// base URL for the API
/// </summary>
@@ -128,7 +133,7 @@ namespace PocketSharp
/// <param name="parameters">Additional POST parameters</param>
/// <returns></returns>
/// <exception cref="APIException">No access token available. Use authentification first.</exception>
protected T GetResource<T>(string method, List<Parameter> parameters) where T : class, new()
protected T GetResource<T>(string method, List<Parameter> 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<T>(request);
+1 -3
View File
@@ -60,9 +60,7 @@
<Compile Include="PocketClient.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Utilties\" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>