【问题标题】:Linq to Twitter status updateLinq to Twitter 状态更新
【发布时间】:2014-10-28 13:40:38
【问题描述】:

这是我的代码

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

您的凭据不允许在

访问此资源

var tweet = twitterCtx.UpdateStatus("Hello world");

 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 是否正确,并且我还给了我的应用读写权限。我能够获得以前的状态,用户名,但我无法发布新状态

【问题讨论】:

    标签: c# twitter


    【解决方案1】:

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

    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 的实例,auth,你可以像这样使用它:

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

    * 更新 *

    前面的代码来自旧版本的 LINQ to 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 文件夹中有演示。

    【讨论】:

    • LinqToTwitter API 已更改(现在所有调用 Async 和中断名称更改);这些都不再有效,LinqToTwitter 网站上的示例/源代码都充满了相同的错误。如果有人知道更新版本,请发布。
    • @Rudy 我更新了我的答案。 API 文档 (linqtotwitter.codeplex.com/…) 包含 2.x 和 3.x 版本的查询,因此您可以在遇到旧代码示例时看到差异。
    【解决方案2】:

    TwitterContext 上有一个名为 UpdateStatus 的方法,您可以像这样使用它:

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

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

    【讨论】:

    • 从哪里获得 PIN 选项?
    • 我如何知道 Twitter 上的状态是否更新。
    猜你喜欢
    • 2012-07-02
    • 2012-03-13
    • 2016-03-12
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多