【问题标题】:authorization error using linqtotwitter for streaming api使用 linqtotwitter 进行流 api 的授权错误
【发布时间】:2017-06-15 20:20:19
【问题描述】:

我正在尝试使用 LinqToTwitter 库和以下 c# 代码获取推文流,但出现此错误:

错误 401 未经授权

public static SingleUserAuthorizer auth;
    static void Main(string[] args)
    {
        Task task = new Task(getStreamOfTweets);
        task.Start();
        task.Wait();
        Console.ReadLine();
    }

    static async void getStreamOfTweets()
    {
        auth = new SingleUserAuthorizer
        {
            CredentialStore = new SingleUserInMemoryCredentialStore
            {
                ConsumerKey = CUSTOMER_KEY,
                ConsumerSecret = CUSTOMER_SECRET,
                AccessToken = ACCESS_TOKEN,
                AccessTokenSecret = ACCESS_TOKEN_SECRET
            }
        };
        var context = new TwitterContext(auth);
        int count = 0;
        await (from strm in context.Streaming
               where strm.Type == StreamingType.Filter
               && strm.Track == "federer"
               select strm)
            .StartAsync(async strm =>
            {
                string message =
                    string.IsNullOrEmpty(strm.Content) ?
                        "Keep-Alive" : strm.Content;
                Console.WriteLine(
                    (count + 1).ToString() +
                    ". " + DateTime.Now +
                    ": " + message + "\n");

                if (count++ == 5)
                    strm.CloseStream();
            });
    }

注意事项:

  • twitter 应用中的权限是“读、写和访问直接消息”

  • 我可以通过 REST API 正确获取推文

【问题讨论】:

    标签: c# twitter twitter-streaming-api linq-to-twitter


    【解决方案1】:

    请查看LINQ to Twitter FAQ,其中包含有关解决 401 错误的详细部分。也就是说,如果它适用于 REST API,但不适用于 Streaming,则可能会缩小尝试的选择范围。首先要尝试以下几件事:

    1. 仔细检查键以确保您没有意外添加空格或在末端丢失字符。
    2. 请稍等片刻后再重试,因为有时访问次数过多或尝试失败可能会导致他们在一段时间内拒绝您的连接。
    3. 回复:#2,尝试另一个流,例如 Sample。
    4. 使用 OAuth 需要考虑很多因素,因此请仔细阅读该列表,以防您可能遗漏了什么。

    【讨论】:

      【解决方案2】:

      这个问题是因为windows的时间不对。

      【讨论】:

        【解决方案3】:

        如果您从 Linq"Twitter 示例代码中复制并粘贴,请确保您已正确设置所有键:

          ConsumerKey 
          ConsumerSecret 
          AccessToken 
          AccessTokenSecret 
        

        然后不要使用“仅应用程序”身份验证,使用“用户身份验证”而不是使用上述所有密钥的流式传输。

        【讨论】:

          【解决方案4】:

          问题是因为PC中的时间错误

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-02-17
            • 2013-07-05
            • 2020-10-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-09-22
            相关资源
            最近更新 更多