LinqToTwitter OAuth未经授权

时间:2013-07-08 12:32:29

标签: c# twitter linq-to-twitter

我正在关注LinqToTwitter文档中的文章:

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

为了允许用户使用Twitter登录我的C#Web应用程序。

这是我的代码

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);      
}

但是我从这一行获得了一个Http 401:

auth.BeginAuthorization(Request.Url);    

这是堆栈跟踪:

 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)

凭据包含我的Twitter应用程序集的ConsumerKey和ConsumerSecret。

如何授权我的用户?

1 个答案:

答案 0 :(得分:1)

Twitter API返回HTTP 401 Unauthorized有很多原因。我已经整理了一个LINQ to Twitter FAQ,它有一个解决401的项目。

LINQ to Twitter Downloads页面有一个可以尝试的WebFormsDemos项目,还有一个带有可下载源代码的WebForms演示。