0

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

Hasan Mhd Amin
  • 220
  • 2
  • 11

4 Answers4

0

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.
Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
0

this issue happens because the time of windows was wrong.

Hasan Mhd Amin
  • 220
  • 2
  • 11
0

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.

mhatch
  • 4,441
  • 6
  • 36
  • 62
Yemoku
  • 126
  • 1
  • 5
0

the issue was because the timing in PC was wrong

Hasan Mhd Amin
  • 220
  • 2
  • 11