Question

I'm following the article in the LinqToTwitter documentation:

http://linqtotwitter.codeplex.com/wikipage?title=Implementing%20OAuth%20for%20ASP.NET%20Web%20Forms&referringTitle=Learning%20to%20use%20OAuth

in order to allow users to log into my C# web application with Twitter.

Here's my code

private WebAuthorizer auth;
private TwitterContext twitterCtx;

protected void Page_Load(object sender, EventArgs e)
{
  IOAuthCredentials credentials = new SessionStateCredentials();

  if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
  {
    credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
    credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
  }

  auth = new WebAuthorizer
  {
    Credentials = credentials,
    PerformRedirect = authUrl => Response.Redirect(authUrl)
  };

  if (!Page.IsPostBack)
  {
    auth.CompleteAuthorization(Request.Url);
  }

  twitterCtx = new TwitterContext(auth);

}


protected void AuthorizeTwitterButton_Click(object sender, EventArgs e)
{
  auth.BeginAuthorization(Request.Url);      
}

However I'm getting a Http 401 from the line:

auth.BeginAuthorization(Request.Url);    

Here's the stack trace:

 at LinqToTwitter.OAuthTwitter.WebResponseGet(HttpWebRequest webRequest)
   at LinqToTwitter.OAuthTwitter.WebRequest(HttpMethod method, String url, String authHeader, IDictionary`2 postData)
   at LinqToTwitter.OAuthTwitter.OAuthWebRequest(HttpMethod method, Request request, IDictionary`2 postData, String callback)
   at LinqToTwitter.OAuthTwitter.AuthorizationLinkGet(String requestToken, String authorizeUrl, String callback, Boolean forceLogin, AuthAccessType authAccessToken)
   at LinqToTwitter.WebAuthorizer.BeginAuthorization(Uri callback, Boolean forceLogin)
   at LinqToTwitter.WebAuthorizer.BeginAuthorization(Uri callback)
   at TwitterTest.Default.AuthorizeTwitterButton_Click(Object sender, EventArgs e) in c:\Users\agriffiths.SDASOL\Documents\Visual Studio 11\Projects\TwitterTest\TwitterTest\Default.aspx.cs:line 47
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

The credentials have both the ConsumerKey and ConsumerSecret for my Twitter application set.

How can I authorise my users?

Was it helpful?

Solution

There are many reasons that Twitter API returns the HTTP 401 Unauthorized. I've put together a LINQ to Twitter FAQ that has an item on resolving 401's.

The LINQ to Twitter Downloads page has a WebFormsDemos project you can try and there's also a WebForms demo with the downloadable source code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top