desmondmorris / node-twitter

Client library for the Twitter REST and Streaming API's.
https://www.npmjs.com/package/twitter
MIT License
1.27k stars 237 forks source link

401 on follow in streaming #280

Closed ygalanter closed 6 years ago

ygalanter commented 6 years ago

I've seen similar question asked before, but I tried all the recommendations and still no luck. REST API works, streaming works for "track" but returns 401 for "follow". Help!

ygalanter commented 6 years ago

Never mind. Was being and idiot and using twitter screen names instead of IDs.

yousefissa commented 6 years ago

Can you post your code? Still getting this with IDs.

ygalanter commented 6 years ago

Are you using actual numeric IDs? I usually use services like http://gettwitterid.com/ to get numeric id from a twitter handle.

yousefissa commented 6 years ago

I managed to fix it by removing a parameter from the function call. It was weird.

I got it working now but it seems to catch tweets where people @ someone as well. Do you know if I can just capture my specific timeline?

ygalanter commented 6 years ago

Yes you need to filter out mentions (and if you want - retweets too). Here's how I did it (it's designed to track multiple users, but can be used for one too):

// Twitter IDs of tracked users
var users = ['243235235','235923485', '52347345'];

var stream = tw_client.stream('statuses/filter', {follow: users.join(',')});

stream.on('data', function(tweet) {
    // if tweet is not originated from tracked user (mention) or retweet - exit
    if (!users.find(el => tweet.user.id_str == el && !tweet.retweeted_status)) return;

    // continue code
}
yousefissa commented 6 years ago

Sweet! I managed to do it using this code.

idList.indexOf(tweet.user.id_str) > -1

Since I don't want to catch retweets.

Thank you!

ygalanter commented 6 years ago

No problem. Glad it worked for you :)