|
| 1 | +// Follow a list using PIN-based OAuth to authorize the user |
| 2 | +// https://developer.twitter.com/en/docs/twitter-api/lists/manage-lists/quick-start |
| 3 | +const got = require("got"); |
| 4 | +const crypto = require("crypto"); |
| 5 | +const OAuth = require("oauth-1.0a"); |
| 6 | +const qs = require("querystring"); |
| 7 | + |
| 8 | +const readline = require("readline").createInterface({ |
| 9 | + input: process.stdin, |
| 10 | + output: process.stdout, |
| 11 | +}); |
| 12 | + |
| 13 | +// The code below sets the consumer key and consumer secret from your environment variables |
| 14 | +// To set environment variables on macOS or Linux, run the export commands below from the terminal: |
| 15 | +// export CONSUMER_KEY='YOUR-KEY' |
| 16 | +// export CONSUMER_SECRET='YOUR-SECRET' |
| 17 | +const consumer_key = process.env.CONSUMER_KEY; |
| 18 | +const consumer_secret = process.env.CONSUMER_SECRET; |
| 19 | + |
| 20 | +// Be sure to replace your-user-id with your own user ID or one of an authenticated user |
| 21 | +// You can find a user ID by using the user lookup endpoint |
| 22 | +const id = "your-user-id"; |
| 23 | + |
| 24 | +// Be sure to add replace list-id-to-follow with the list id you wish to follow. |
| 25 | + |
| 26 | +const data = { |
| 27 | + list_id: "list-id-to-follow", |
| 28 | +}; |
| 29 | + |
| 30 | +const endpointURL = `https://api.twitter.com/2/users/${id}/followed_lists`; |
| 31 | + |
| 32 | +// this example uses PIN-based OAuth to authorize the user |
| 33 | +const requestTokenURL = |
| 34 | + "https://api.twitter.com/oauth/request_token?oauth_callback=oob"; |
| 35 | +const authorizeURL = new URL("https://api.twitter.com/oauth/authorize"); |
| 36 | +const accessTokenURL = "https://api.twitter.com/oauth/access_token"; |
| 37 | +const oauth = OAuth({ |
| 38 | + consumer: { |
| 39 | + key: consumer_key, |
| 40 | + secret: consumer_secret, |
| 41 | + }, |
| 42 | + signature_method: "HMAC-SHA1", |
| 43 | + hash_function: (baseString, key) => |
| 44 | + crypto.createHmac("sha1", key).update(baseString).digest("base64"), |
| 45 | +}); |
| 46 | + |
| 47 | +async function input(prompt) { |
| 48 | + return new Promise(async (resolve, reject) => { |
| 49 | + readline.question(prompt, (out) => { |
| 50 | + readline.close(); |
| 51 | + resolve(out); |
| 52 | + }); |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +async function requestToken() { |
| 57 | + const authHeader = oauth.toHeader( |
| 58 | + oauth.authorize({ |
| 59 | + url: requestTokenURL, |
| 60 | + method: "POST", |
| 61 | + }) |
| 62 | + ); |
| 63 | + |
| 64 | + const req = await got.post(requestTokenURL, { |
| 65 | + headers: { |
| 66 | + Authorization: authHeader["Authorization"], |
| 67 | + }, |
| 68 | + }); |
| 69 | + if (req.body) { |
| 70 | + return qs.parse(req.body); |
| 71 | + } else { |
| 72 | + throw new Error("Cannot get an OAuth request token"); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +async function accessToken({ oauth_token, oauth_token_secret }, verifier) { |
| 77 | + const authHeader = oauth.toHeader( |
| 78 | + oauth.authorize({ |
| 79 | + url: accessTokenURL, |
| 80 | + method: "POST", |
| 81 | + }) |
| 82 | + ); |
| 83 | + const path = `https://api.twitter.com/oauth/access_token?oauth_verifier=${verifier}&oauth_token=${oauth_token}`; |
| 84 | + const req = await got.post(path, { |
| 85 | + headers: { |
| 86 | + Authorization: authHeader["Authorization"], |
| 87 | + }, |
| 88 | + }); |
| 89 | + if (req.body) { |
| 90 | + return qs.parse(req.body); |
| 91 | + } else { |
| 92 | + throw new Error("Cannot get an OAuth request token"); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +async function getRequest({ oauth_token, oauth_token_secret }) { |
| 97 | + const token = { |
| 98 | + key: oauth_token, |
| 99 | + secret: oauth_token_secret, |
| 100 | + }; |
| 101 | + |
| 102 | + const authHeader = oauth.toHeader( |
| 103 | + oauth.authorize( |
| 104 | + { |
| 105 | + url: endpointURL, |
| 106 | + method: "POST", |
| 107 | + }, |
| 108 | + token |
| 109 | + ) |
| 110 | + ); |
| 111 | + |
| 112 | + const req = await got.post(endpointURL, { |
| 113 | + json: data, |
| 114 | + responseType: "json", |
| 115 | + headers: { |
| 116 | + Authorization: authHeader["Authorization"], |
| 117 | + "user-agent": "v2followListJS", |
| 118 | + "content-type": "application/json", |
| 119 | + accept: "application/json", |
| 120 | + }, |
| 121 | + }); |
| 122 | + if (req.body) { |
| 123 | + return req.body; |
| 124 | + } else { |
| 125 | + throw new Error("Unsuccessful request"); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +(async () => { |
| 130 | + try { |
| 131 | + // Get request token |
| 132 | + const oAuthRequestToken = await requestToken(); |
| 133 | + // Get authorization |
| 134 | + authorizeURL.searchParams.append( |
| 135 | + "oauth_token", |
| 136 | + oAuthRequestToken.oauth_token |
| 137 | + ); |
| 138 | + console.log("Please go here and authorize:", authorizeURL.href); |
| 139 | + const pin = await input("Paste the PIN here: "); |
| 140 | + // Get the access token |
| 141 | + const oAuthAccessToken = await accessToken(oAuthRequestToken, pin.trim()); |
| 142 | + // Make the request |
| 143 | + const response = await getRequest(oAuthAccessToken); |
| 144 | + console.dir(response, { |
| 145 | + depth: null, |
| 146 | + }); |
| 147 | + } catch (e) { |
| 148 | + console.log(e); |
| 149 | + process.exit(-1); |
| 150 | + } |
| 151 | + process.exit(); |
| 152 | +})(); |
0 commit comments