Created
September 13, 2011 21:48
-
-
Save jsclayton/1215267 to your computer and use it in GitHub Desktop.
UserVoice OAuth helper for SSO
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
using System; | |
using RestSharp; | |
using RestSharp.Authenticators; | |
namespace UserVoice.Api | |
{ | |
public class UserVoiceOAuthToken | |
{ | |
public string OauthToken { get; set; } | |
public string OauthTokenSecret { get; set; } | |
} | |
public class UserVoiceOAuthHelper | |
{ | |
private readonly string url; | |
private readonly string consumerKey; | |
private readonly string consumerSecret; | |
public UserVoiceOAuthHelper(string url, string consumerKey, string consumerSecret) | |
{ | |
this.url = url; // http://{youraccount}.uservoice.com/api/v1/ | |
this.consumerKey = consumerKey; | |
this.consumerSecret = consumerSecret; | |
} | |
public UserVoiceOAuthToken GetRequestToken() | |
{ | |
var oauth = OAuth1Authenticator.ForRequestToken(consumerKey, consumerSecret); | |
var client = new RestClient(url) { Authenticator = oauth }; | |
var request = new RestRequest("oauth/request_token.json", Method.POST) { RootElement = "token" }; | |
var response = client.Execute<UserVoiceOAuthToken>(request); | |
if (response.Data == null) | |
throw new Exception(response.ErrorMessage, response.ErrorException); | |
return response.Data; | |
} | |
public UserVoiceOAuthToken Authorize(string sso, UserVoiceOAuthToken requestToken) | |
{ | |
var oauth = OAuth1Authenticator.ForProtectedResource(consumerKey, consumerSecret, null, null); | |
var client = new RestClient(url) { Authenticator = oauth }; | |
var request = new RestRequest("oauth/authorize.json", Method.POST) { RootElement = "token" }; | |
request.AddParameter("sso", sso); // DO NOT URL ENCODE THE SSO TOKEN!!! | |
request.AddParameter("request_token", requestToken.OauthToken); | |
var response = client.Execute<UserVoiceOAuthToken>(request); | |
if (response.Data == null) | |
throw new Exception(response.ErrorMessage, response.ErrorException); | |
return response.Data; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment