2013-03-03 75 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模式。
相关问题