skip to Main Content

i’m trying to get a stream of tweets using LinqToTwitter library and the below c# code, but i get this error:

Error 401 Unauthorized

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();
            });
    }

notes:

  • the permission in twitter app is “Read, Write and Access direct messages”

  • i can get tweet by REST API correctly

4

Answers


  1. Chosen as BEST ANSWER

    this issue happens because the time of windows was wrong.


  2. Please review the LINQ to Twitter FAQ, which has an extensive section on resolving 401 errors. That said, if it’s working for the REST API, but not Streaming, that might narrow the options to try. Here are a couple things to try first:

    1. Double check the keys to make sure you didn’t accidentally add a space or lose a character on the ends.
    2. Give a little time before trying again because sometimes too many accesses or failed attempts might cause them to deny your connection for a certain period of time.
    3. Re: #2, try another stream, like Sample.
    4. There are a lot of moving parts in play for using OAuth, so work through that list in case you might have missed something.
    Login or Signup to reply.
  3. If you copied & pasted from the Linq”Twitter sample code, make sure you have set properly all the keys:

      ConsumerKey 
      ConsumerSecret 
      AccessToken 
      AccessTokenSecret 
    

    Then dont use “Application only” auth, use “user auth” instead for streaming which uses all of the above keys.

    Login or Signup to reply.
  4. the issue was because the timing in PC was wrong

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search