forked from xdevplatform/Twitter-API-v2-sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-owned-list-lookup.js
47 lines (41 loc) · 1.29 KB
/
user-owned-list-lookup.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
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;
const id = "user-id";
const endpointURL = `https://api.twitter.com/2/users/${id}/owned_lists`;
async function getRequest() {
// These are the parameters for the API request
// by default, only the List ID and name are returned
const params = {
"list.fields": "owner_id", // Edit optional query parameters here
expansions: "owner_id", // expansions is used to include the user object
"user.fields": "created_at,verified", // 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": "v2ListLookupJS",
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();
})();