forked from xdevplatform/Twitter-API-v2-sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliking_users.js
49 lines (42 loc) · 1.36 KB
/
liking_users.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
const needle = require("needle");
// The code below sets the bearer token from your environment variables
// To set environment variables on macOS or Linux, run the export command below from the terminal:
// export BEARER_TOKEN='YOUR-TOKEN'
const token = process.env.BEARER_TOKEN;
// You can replace the ID given with the Tweet ID you wish to like.
// You can find an ID by using the Tweet lookup endpoint
const id = "1354143047324299264";
const endpointURL = `https://api.twitter.com/2/tweets/${id}/liking_users`;
async function getRequest() {
// These are the parameters for the API request
// by default, only the Tweet ID and text are returned
const params = {
"tweet.fields": "lang,author_id", // Edit optional query parameters here
"user.fields": "created_at", // Edit optional query parameters here
};
// this is the HTTP header that adds bearer token authentication
const res = await needle("get", endpointURL, params, {
headers: {
"User-Agent": "v2LikingUsersJS",
authorization: `Bearer ${token}`
},
});
if (res.body) {
return res.body;
} else {
throw new Error("Unsuccessful request");
}
}
(async () => {
try {
// Make request
const response = await getRequest();
console.dir(response, {
depth: null,
});
} catch (e) {
console.log(e);
process.exit(-1);
}
process.exit();
})();