Skip to content

Commit 39f0cfd

Browse files
authored
Create FullArchiveSearchDemo.java
fullarchivesearchdemo Signed-off-by: Rahul Chaube <157899057+Rahulchaube1@users.noreply.github.com>
1 parent 8c63446 commit 39f0cfd

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

FullArchiveSearchDemo.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.io.IOException;
2+
import java.net.URISyntaxException;
3+
import java.util.ArrayList;
4+
5+
import org.apache.http.HttpEntity;
6+
import org.apache.http.HttpResponse;
7+
import org.apache.http.NameValuePair;
8+
import org.apache.http.client.HttpClient;
9+
import org.apache.http.client.config.CookieSpecs;
10+
import org.apache.http.client.config.RequestConfig;
11+
import org.apache.http.client.methods.HttpGet;
12+
import org.apache.http.client.utils.URIBuilder;
13+
import org.apache.http.impl.client.HttpClients;
14+
import org.apache.http.message.BasicNameValuePair;
15+
import org.apache.http.util.EntityUtils;
16+
17+
/**
18+
* Demonstrates the use of the Full Archive Search endpoint for Twitter API v2.
19+
*/
20+
public class FullArchiveSearchDemo {
21+
22+
// To set your environment variables in your terminal, run the following line:
23+
// export BEARER_TOKEN='<your_bearer_token>'
24+
25+
public static void main(String[] args) throws IOException, URISyntaxException {
26+
String bearerToken = System.getenv("BEARER_TOKEN");
27+
28+
if (bearerToken != null && !bearerToken.isEmpty()) {
29+
// Replace the search term with a term of your choice
30+
String searchTerm = "from:TwitterDev OR from:SnowBotDev OR from:DailyNASA";
31+
try {
32+
String response = search(searchTerm, bearerToken);
33+
System.out.println("Search Response:");
34+
System.out.println(response);
35+
} catch (Exception e) {
36+
System.err.println("An error occurred during the API call: " + e.getMessage());
37+
e.printStackTrace();
38+
}
39+
} else {
40+
System.out.println("There was a problem getting your bearer token. "
41+
+ "Please make sure you set the BEARER_TOKEN environment variable.");
42+
}
43+
}
44+
45+
/**
46+
* Calls the Full Archive Search endpoint with the search term passed as a query parameter.
47+
*
48+
* @param searchString The query string for the search
49+
* @param bearerToken The Bearer Token for authentication
50+
* @return The search response as a String
51+
* @throws IOException
52+
* @throws URISyntaxException
53+
*/
54+
private static String search(String searchString, String bearerToken) throws IOException, URISyntaxException {
55+
HttpClient httpClient = HttpClients.custom()
56+
.setDefaultRequestConfig(RequestConfig.custom()
57+
.setCookieSpec(CookieSpecs.STANDARD).build())
58+
.build();
59+
60+
URIBuilder uriBuilder = new URIBuilder("https://api.twitter.com/2/tweets/search/all");
61+
ArrayList<NameValuePair> queryParameters = new ArrayList<>();
62+
queryParameters.add(new BasicNameValuePair("query", searchString));
63+
queryParameters.add(new BasicNameValuePair("max_results", "10")); // Adjust max_results as needed
64+
uriBuilder.addParameters(queryParameters);
65+
66+
HttpGet httpGet = new HttpGet(uriBuilder.build());
67+
httpGet.setHeader("Authorization", String.format("Bearer %s", bearerToken));
68+
httpGet.setHeader("Content-Type", "application/json");
69+
70+
HttpResponse response = httpClient.execute(httpGet);
71+
HttpEntity entity = response.getEntity();
72+
73+
if (entity != null) {
74+
return EntityUtils.toString(entity, "UTF-8");
75+
} else {
76+
throw new IOException("No response received from the API.");
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)