|
| 1 | +const { Client, auth } = require("twitter-api-sdk"); |
| 2 | + |
| 3 | +const readline = require("readline").createInterface({ |
| 4 | + input: process.stdin, |
| 5 | + output: process.stdout, |
| 6 | +}); |
| 7 | + |
| 8 | +//Helper function to parse callback |
| 9 | +const getQueryStringParams = (query) => { |
| 10 | + return query |
| 11 | + ? (/^[?#]/.test(query) ? query.slice(1) : query) |
| 12 | + .split(/[\?\&]/) |
| 13 | + .reduce((params, param) => { |
| 14 | + let [key, value] = param.split("="); |
| 15 | + params[key] = value |
| 16 | + ? decodeURIComponent(value.replace(/\+/g, " ")) |
| 17 | + : ""; |
| 18 | + return params; |
| 19 | + }, {}) |
| 20 | + : {}; |
| 21 | +}; |
| 22 | + |
| 23 | +//Helper terminal input function |
| 24 | +async function input(prompt) { |
| 25 | + return new Promise(async (resolve, reject) => { |
| 26 | + readline.question(prompt, (out) => { |
| 27 | + readline.close(); |
| 28 | + resolve(out); |
| 29 | + }); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +// The code below sets the consumer key and consumer secret from your environment variables |
| 34 | +// To set environment variables on macOS or Linux, run the export commands below from the terminal: |
| 35 | +// export CLIENT_ID='YOUR-CLIENT-ID' |
| 36 | +// export CLIENET_SECRET='YOUR-CLIENT-SECRET' |
| 37 | +const CLIENT_ID = process.env.CLIENT_ID; |
| 38 | +const CLIENT_SECRET = process.env.CLIENT_SECRET; |
| 39 | + |
| 40 | +// Optional parameters for additional payload data |
| 41 | +const params = { |
| 42 | + expansions: "author_id", |
| 43 | + "user.fields": ["username", "created_at"], |
| 44 | + "tweet.fields": ["geo", "entities", "context_annotations"], |
| 45 | +}; |
| 46 | + |
| 47 | +(async () => { |
| 48 | + const authClient = new auth.OAuth2User({ |
| 49 | + client_id: CLIENT_ID, |
| 50 | + client_secret: CLIENT_SECRET, |
| 51 | + callback: "https://www.example.com/oauth", |
| 52 | + scopes: ["tweet.read", "users.read", "bookmark.read"], |
| 53 | + }); |
| 54 | + |
| 55 | + const client = new Client(authClient); |
| 56 | + const STATE = "my-state"; |
| 57 | + |
| 58 | + //Get authorization |
| 59 | + const authUrl = authClient.generateAuthURL({ |
| 60 | + state: STATE, |
| 61 | + code_challenge: "challenge", |
| 62 | + }); |
| 63 | + |
| 64 | + console.log(`Please go here and authorize:`, authUrl); |
| 65 | + |
| 66 | + //Input users callback url in termnial |
| 67 | + const redirectCallback = await input("Paste the redirected callback here: "); |
| 68 | + |
| 69 | + try { |
| 70 | + //Parse callback |
| 71 | + const { state, code } = getQueryStringParams(redirectCallback); |
| 72 | + if (state !== STATE) { |
| 73 | + console.log("State isn't matching"); |
| 74 | + } |
| 75 | + //Gets access token |
| 76 | + await authClient.requestAccessToken(code); |
| 77 | + |
| 78 | + //Get the user ID |
| 79 | + const { |
| 80 | + data: { id }, |
| 81 | + } = await client.users.findMyUser(); |
| 82 | + |
| 83 | + //Makes api call |
| 84 | + const getBookmark = await client.bookmarks.getUsersIdBookmarks(id, params); |
| 85 | + console.dir(getBookmark, { |
| 86 | + depth: null, |
| 87 | + }); |
| 88 | + process.exit(); |
| 89 | + } catch (error) { |
| 90 | + console.log(error); |
| 91 | + } |
| 92 | +})(); |
0 commit comments