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