【问题标题】:LinqToTwitter OAuth UnauthorisedLinqToTwitter OAuth 未经授权
【发布时间】:2013-07-05 18:55:37
【问题描述】:

我正在关注 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。

如何授权我的用户?

【问题讨论】:

    标签: c# twitter linq-to-twitter


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!最后结果证明是 CallbackURL 未设置和允许通过应用程序进行身份验证的选项被禁用的组合。
    • 好点 - 我更新了常见问题解答,表明问题可能是由多个问题引起的。
    猜你喜欢
    • 2019-09-09
    • 1970-01-01
    • 2018-07-04
    • 2012-04-08
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2012-12-20
    • 2012-01-28
    相关资源
    最近更新 更多