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