|
1 | | -# node-twitter-api |
2 | | -Twitter api v1 and v2 client for node |
| 1 | +# Twitter API V2 |
| 2 | + |
| 3 | +WIP |
| 4 | + |
| 5 | +Twitter api v2 (and v1 in the future) client for node |
| 6 | + |
| 7 | +## Why? |
| 8 | + |
| 9 | +- The main libraries (twit/twitter) were not updated in a while |
| 10 | + |
| 11 | +- I don't think a Twitter library need many dependencies |
| 12 | + |
| 13 | +They caused me some frustration: |
| 14 | +- They don't support video upload in a simple way |
| 15 | +- They don't explain well the "link" auth process |
| 16 | +- They don't support yet Twitter API V2 |
| 17 | +- They could have more helpers (for pagination, rate limit, ...) |
| 18 | +- Typings could make the difference between read/write app |
| 19 | + |
| 20 | +## Goals: |
| 21 | + |
| 22 | +- [ ] bearer token auth |
| 23 | +- [ ] token auth |
| 24 | +- [ ] link auth |
| 25 | +- [ ] read/write/DM aware typing |
| 26 | +- [ ] get/post methods |
| 27 | +- [ ] Twitter API V2 tweets methods |
| 28 | +- [ ] Twitter API V2 users methods |
| 29 | +- [ ] Auto pagination |
| 30 | +- [ ] Error code enums |
| 31 | + |
| 32 | +```typescript |
| 33 | +import TwitterApi, { TwitterErrors } from 'twitter-api-v2'; |
| 34 | + |
| 35 | +// bearer token auth (with V2) |
| 36 | +const twitterClient = new TwitterApi('<YOUR_APP_USER_TOKEN>'); |
| 37 | + |
| 38 | +// token auth |
| 39 | +const twitterClient = new TwitterApi({ |
| 40 | + appKey: '<YOUR-TWITTER-APP-TOKEN>', |
| 41 | + appSecret: '<YOUR-TWITTER-APP-SECERT>', |
| 42 | + accesToken: '<YOUR-TWITTER-APP-TOKEN>', |
| 43 | + accessSecret: '<YOUR-TWITTER-APP-SECERT>', |
| 44 | + }); |
| 45 | + |
| 46 | +// link auth |
| 47 | +const twitterClient = new TwitterApi({ |
| 48 | + appKey: '<YOUR-TWITTER-APP-TOKEN>', |
| 49 | + appSecret: '<YOUR-TWITTER-APP-SECERT>', |
| 50 | +}); |
| 51 | + |
| 52 | +const authLink = await twitterClient.generateAuthLink(); |
| 53 | +// ... redirected to https://website.com?oauth_token=XXX&oauth_verifier=XXX |
| 54 | +const { usertoken, userSecret } = twitterClient.login('<THE_OAUTH_TOKEN>', '<THE_OAUTH_VERIFIER>'); |
| 55 | + |
| 56 | +// Tell typescript it's a readonly app |
| 57 | +const twitterClient = new TwitterApi(xxx).readOnly; |
| 58 | + |
| 59 | +// Search for tweets |
| 60 | +const tweets = await twitterClient.tweets.search('nodeJS', { max_results: 100 }); |
| 61 | + |
| 62 | +// Or do it your way (however, with no result typed) |
| 63 | +const tweets = await twitterClient.v2.get('tweets/search/recent', {query: 'nodeJS', max_results: '100'}); |
| 64 | +const tweets = await twitterClient.get('https://api.twitter.com/2/tweets/search/recent?query=nodeJS&max_results=100'); |
| 65 | + |
| 66 | +// Auto-paginate |
| 67 | +// (also checks if rate limits will be enough after the first request) |
| 68 | +const manyTweets = await twitterClient.tweets.search('nodeJS').fetchLast(10000); |
| 69 | + |
| 70 | +// Manage errors |
| 71 | +try { |
| 72 | + const manyTweets = await twitterClient.tweets.search('nodeJS').fetchLast(100000000); |
| 73 | +} catch(e) { |
| 74 | + if (e.errorCode === TwitterErrors.RATE_LIMIT_EXCEEDED) { |
| 75 | + console.log('please try again later!'); |
| 76 | + } else { |
| 77 | + throw e; |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
0 commit comments