2013-07-08 178 views
1

我正在關注的LinqToTwitter文檔中的文章:LinqToTwitter的OAuth未經授權

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) 

憑證有兩種ConsumerKey和ConsumerSecret用於我的Twitter應用程序集。

我該如何授權我的用戶?

回答

1

有很多原因,Twitter API返回HTTP 401未經授權。我已經把一個LINQ to Twitter FAQ放在一起,有一個解決401的項目。

從LINQ到Twitter Downloads頁面有一個WebFormsDemos項目,你可以嘗試,也有一個WebForms演示與可下載的源代碼。

+0

謝謝!最後它證明了未設置Call​​backURL的組合以及允許通過應用程序進行身份驗證被禁用的選項。 – Liath

+0

好點 - 我更新了常見問題表明問題可能由多個問題引起。 –

相關問題