2013-03-03 61 views
0

我知道這已被問過很多次,但我已經使用linqtotwitter文檔和示例中的信息以及此處的其他帖子以瞭解更多信息。我可以讓我的新應用發送推文,但只有讓我通過推文授權頁面,我才能點擊授權按鈕繼續。我不能讓我的帳戶使用linqtotwitter自己授權

該應用程序本身的授權很好,所以我不需要我的密碼每次,它只是使用它想要權限的應用程序。

我知道這是因爲我的代碼中沒有我的訪問令牌或訪問令牌的機密。我已經添加了它們,但是每次遇到401未授權(無效或過期的令牌)。

我的代碼如下,也許你可以看到我要去哪裏錯了?

private IOAuthCredentials credentials = new SessionStateCredentials(); 

    private MvcAuthorizer auth; 
    private TwitterContext twitterCtx; 

    public ActionResult Index() 
    { 

     credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"]; 
     credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]; 
     credentials.AccessToken = "MYaccessTOKENhere"; // Remove this line and line below and all works fine with manual authorisation every time its called // 
     credentials.OAuthToken = "MYaccessTOKENsecretHERE"; 

     auth = new MvcAuthorizer 
     { 
      Credentials = credentials 
     }; 

     auth.CompleteAuthorization(Request.Url); 

     if (!auth.IsAuthorized) 
     { 
      Uri specialUri = new Uri(Request.Url.ToString()); 
      return auth.BeginAuthorization(specialUri); 
     } 

     twitterCtx = new TwitterContext(auth); 
     twitterCtx.UpdateStatus("Test Tweet Here"); // This is the line it fails on // 

     return View(); 
    } 

回答

2

下面是對解決401錯誤幫助FAQ:http://linqtotwitter.codeplex.com/wikipage?title=LINQ%20to%20Twitter%20FAQ&referringTitle=Documentation

可能會有所幫助過幾個項目:

  1. 不能鳴叫相同的文字兩次 - 這樣無論是使用相同的文字刪除以前的推文或更改文字。我所有的測試都附加了DateTime.Now.ToString()來避免這個錯誤。
  2. SessionStateCredentials在SessionState中保存憑證。會話狀態的持久性模式默認是InProc,這意味着如果進程回收,會話變量將爲空,這將發生不可預測的情況。您可能需要確保您使用的是StateServer或SQL Server模式。
相關問題