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
this issue happens because the time of windows was wrong.
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:
If you copied & pasted from the Linq”Twitter sample code, make sure you have set properly all the keys:
Then dont use “Application only” auth, use “user auth” instead for streaming which uses all of the above keys.
the issue was because the timing in PC was wrong