2013-05-21 144 views
0

这是我的代码Linq to Twitter状态更新

由于某种原因,我无法更新状态我收到此错误。

您的凭据不允许在

VAR鸣叫= twitterCtx.UpdateStatus( “世界,你好”)访问该资源;

var auth = new ApplicationOnlyAuthorizer 
     { 
      Credentials = new InMemoryCredentials 
      { 
       ConsumerKey = "", 
       ConsumerSecret = "" 
      } 
     }; 

     auth.Authorize(); 
     //auth.Invalidate(); 
     var twitterCtx = new TwitterContext(auth); 
     var tweet = twitterCtx.UpdateStatus("Hello world"); 

我检查了我的ConsumerKey和Secret是正确的,我也给了我的应用程序读写acccess。我能够获得以前的状态,用户名,但我只是无法tweet一个新的状态

回答

0

有名为UpdateStatus的TwitterContext,你会使用这样的方法:

twitterCtx.UpdateStatus(" Your text goes here "); 

你”您需要使用OAuth进行授权才能使用此功能,因此请在演示中使用PIN选项。获得授权后,您可以致电UpdateStatus。

+0

从哪里获得PIN码选项? –

+0

我怎么知道状态是否在Twitter上更新。 –

1

应用程序只有授权只能执行应用程序级别的操作。这与其他允许您代表用户操作的授权人不同。逻辑是用户拥有一个账户,但一个应用程序没有。因此,您无法代表应用程序发送推文,因为推文无法分配到任何地方。但是,如果您代表用户发送推文,推文会进入该用户的状态列表(他们的时间表)。

LINQ to Twitter有各种授权人,您可以通过下载特定于您正在使用的技术的样本来查看它们。可下载的源代码也有样本。下面是如何使用PIN授权的例子:

static ITwitterAuthorizer DoPinOAuth() 
    { 
     // validate that credentials are present 
     if (ConfigurationManager.AppSettings["twitterConsumerKey"].IsNullOrWhiteSpace() || 
      ConfigurationManager.AppSettings["twitterConsumerSecret"].IsNullOrWhiteSpace()) 
     { 
      Console.WriteLine("You need to set twitterConsumerKey and twitterConsumerSecret in App.config/appSettings. Visit http://dev.twitter.com/apps for more info.\n"); 
      Console.Write("Press any key to exit..."); 
      Console.ReadKey(); 
      return null; 
     } 

     // configure the OAuth object 
     var auth = new PinAuthorizer 
     { 
      Credentials = new InMemoryCredentials 
      { 
       ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"], 
       ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"] 
      }, 
      AuthAccessType = AuthAccessType.NoChange, 
      UseCompression = true, 
      GoToTwitterAuthorization = pageLink => Process.Start(pageLink), 
      GetPin =() => 
      { 
       // this executes after user authorizes, which begins with the call to auth.Authorize() below. 
       Console.WriteLine("\nAfter authorizing this application, Twitter will give you a 7-digit PIN Number.\n"); 
       Console.Write("Enter the PIN number here: "); 
       return Console.ReadLine(); 
      } 
     }; 

     // start the authorization process (launches Twitter authorization page). 
     auth.Authorize(); 
     return auth; 
    } 

这个方法返回PinAuthorizer的一个实例,身份验证,你可以使用它是这样的:

PinAuthorizer auth = DoPinAuth(); 
var ctx = new TwitterContext(auth); 
ctx.UpdateStatus("Hello LINQ to Twitter!"); 

*更新*

上面的代码是从旧版本的LINQ到Twitter。下面是如何与新的异步版本做一个例子:

static IAuthorizer DoPinOAuth() 
    { 
     var auth = new PinAuthorizer() 
     { 
      CredentialStore = new InMemoryCredentialStore 
      { 
       ConsumerKey = ConfigurationManager.AppSettings["consumerKey"], 
       ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"] 
      }, 
      GoToTwitterAuthorization = pageLink => Process.Start(pageLink), 
      GetPin =() => 
      { 
       Console.WriteLine(
        "\nAfter authorizing this application, Twitter " + 
        "will give you a 7-digit PIN Number.\n"); 
       Console.Write("Enter the PIN number here: "); 
       return Console.ReadLine(); 
      } 
     }; 

     return auth; 
    } 

然后你就可以使用它像这样:

 var auth = DoPinOAuth(); 
     await auth.AuthorizeAsync(); 
     var twitterCtx = new TwitterContext(auth); 
     await twitterCtx.TweetAsync("Hello LINQ to Twitter!"); 

欲了解更多信息,你可以找到DocumentationSource代码。源代码在New \ Demos文件夹中有演示。

+0

LinqToTwitter API已经改变(现在所有的调用异步和破坏名称更改);这一切都不再有效,LinqToTwitter网站上的示例/源代码都有相同的错误。如果有人知道更新的版本,请发布。 – Rudy

+0

@Rudy我更新了我的答案。 API文档(https://linqtotwitter.codeplex.com/wikipage?title=Making%20API%20Calls&referringTitle=Documentation)包含2.x和3.x版本的查询,以便您能够查看大小写你会遇到以前的代码示例。 –