forked from xdevplatform/Twitter-API-v2-sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecent_search.js
51 lines (41 loc) · 1.35 KB
/
recent_search.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
// Search for Tweets within the past seven days
// https://developer.twitter.com/en/docs/twitter-api/tweets/search/quick-start/recent-search
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 endpointUrl = "https://api.twitter.com/2/tweets/search/recent";
async function getRequest() {
// Edit query parameters below
// specify a search query, and any additional fields that are required
// by default, only the Tweet ID and text fields are returned
const params = {
'query': 'from:twitterdev -is:retweet',
'tweet.fields': 'author_id'
}
const res = await needle('get', endpointUrl, params, {
headers: {
"User-Agent": "v2RecentSearchJS",
"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();
})();