Created
March 31, 2014 21:19
-
-
Save abjerner/9902630 to your computer and use it in GitHub Desktop.
Twitter OAuth using Skybrud.Social
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 Skybrud.Social.OAuth; | |
using Skybrud.Social.Twitter; | |
using Skybrud.Social.Twitter.OAuth; | |
using Skybrud.Social.Twitter.Objects; | |
namespace Skybrud.Social.Test.Twitter { | |
public partial class OAuth : System.Web.UI.Page { | |
public string Action { | |
get { return Request.QueryString["do"]; } | |
} | |
public string AuthorizationCode { | |
get { return Request.QueryString["code"]; } | |
} | |
protected void Page_Load(object sender, EventArgs e) { | |
TwitterOAuthClient oauth = new TwitterOAuthClient { | |
ConsumerKey = "", | |
ConsumerSecret = "", | |
Callback = "http://localhost:49681/Twitter/OAuth.aspx?bacon=awesome" | |
}; | |
if (Request.QueryString["do"] == "login") { | |
// Get a request token from the Twitter API | |
OAuthRequestToken token = oauth.GetRequestToken(); | |
// Save the token information to the session so we can grab it later | |
Session[token.Token] = token; | |
// Redirect the user to the authentication page at Twitter.com | |
Response.Redirect(token.AuthorizeUrl); | |
} else if (Request.QueryString["oauth_token"] != null) { | |
// Get OAuth parameters from the query string | |
string oAuthToken = Request.QueryString["oauth_token"]; | |
string oAuthVerifier = Request.QueryString["oauth_verifier"]; | |
// Grab the request token from the session | |
OAuthRequestToken token = Session[oAuthToken] as OAuthRequestToken; | |
if (token == null) { | |
Content.Text = "<p>An error occured. Timeout?</p>"; | |
} else { | |
Content.Text += "<p>Request Token: " + token.Token + "</p>"; | |
Content.Text += "<p>Request Token Secret: " + token.TokenSecret + "</p>"; | |
oauth.Token = token.Token; | |
oauth.TokenSecret = token.TokenSecret; | |
try { | |
OAuthAccessToken accessToken = oauth.GetAccessToken(oAuthVerifier); | |
oauth.Token = accessToken.Token; | |
oauth.TokenSecret = accessToken.TokenSecret; | |
TwitterService service = TwitterService.CreateFromOAuthClient(new TwitterOAuthClient { | |
ConsumerKey = oauth.ConsumerKey, | |
ConsumerSecret = oauth.ConsumerSecret, | |
Token = oauth.Token, | |
TokenSecret = oauth.TokenSecret | |
}); | |
TwitterUser user = service.Account.VerifyCredentials(); | |
Content.Text += "<b>Hi " + (user.Name ?? user.ScreenName) + "</b> (" + user.Id + ")<br />"; | |
Content.Text += "<p>Access Token: " + accessToken.Token + "</p>"; | |
Content.Text += "<p>Access Token Secret: " + accessToken.TokenSecret + "</p>"; | |
} catch (Exception ex) { | |
Content.Text += "<pre style=\"color: red;\">" + ex.GetType().FullName + ": " + ex.Message + "\r\n\r\n" + ex.StackTrace + "</pre>"; | |
} | |
} | |
} else { | |
Content.Text = "<a href=\"OAuth.aspx?do=login\">Login with Twitter</a>"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment