Created
October 10, 2011 11:34
-
-
Save dalager/1275096 to your computer and use it in GitHub Desktop.
Simple Podio Auth with .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PodioClient | |
{ | |
public PodioClient() | |
{ | |
_authkeys = new AuthKeys(); | |
} | |
private DateTime ExpectedExpiry { get; set; } | |
private double ExpiresIn { get; set; } | |
private string RefreshToken { get; set; } | |
private string AccessToken { get; set; } | |
private string BaseUrl = "https://api.podio.com"; | |
private AuthKeys _authkeys; | |
public bool Authenticate(string username, string password) | |
{ | |
var req = new RestRequest("oauth/token"); | |
req.AddParameter("username", username); | |
req.AddParameter("password", password); | |
req.AddParameter("grant_type", "password"); | |
req.Method = Method.POST; | |
var authResponse = Execute<AuthResponse>(req); | |
if (authResponse == null) | |
{ | |
return false; | |
} | |
RefreshToken = authResponse.RefreshToken; | |
AccessToken = authResponse.AccessToken; | |
ExpiresIn = authResponse.ExpiresIn; | |
ExpectedExpiry = DateTime.Now.AddSeconds(authResponse.ExpiresIn); | |
return true; | |
} | |
public T Execute<T>(RestRequest request) where T : new() | |
{ | |
var client = new RestClient { BaseUrl = BaseUrl }; | |
request.AddParameter("client_id", _authkeys.ClientId); | |
request.AddParameter("client_secret", _authkeys.ClientSecret); | |
if (!string.IsNullOrEmpty(AccessToken)) | |
{ | |
request.AddParameter("oauth_token", AccessToken); | |
} | |
var response = client.Execute<T>(request); | |
return response.Data; | |
} | |
} | |
public class AuthResponse | |
{ | |
public string AccessToken{ get; set; } | |
public string TokenType { get; set; } | |
public AuthRef Ref { get; set; } | |
public double ExpiresIn { get; set; } | |
public string RefreshToken { get; set; } | |
public class AuthRef | |
{ | |
public string Type { get; set; } | |
public string Id { get; set; } | |
} | |
} | |
public class AuthKeys | |
{ | |
public AuthKeys() | |
{ | |
ClientId = ConfigurationManager.AppSettings["clientId"]; | |
ClientSecret = ConfigurationManager.AppSettings["clientSecret"]; | |
} | |
public string ClientId { get; set; } | |
public string ClientSecret { get; set; } | |
public override string ToString() | |
{ | |
return string.Format("key {0}, secret {1}", ClientId, ClientSecret); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment