-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubFeedFetcher.java
More file actions
44 lines (36 loc) · 1.63 KB
/
GitHubFeedFetcher.java
File metadata and controls
44 lines (36 loc) · 1.63 KB
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
public class GitHubFeedFetcher {
private static final String GITHUB_API_URL = "***";
private static final String ACCESS_TOKEN = "***";
public static void main(String[] args) {
try {
URL url = new URL(GITHUB_API_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// Set the Authorization header with your personal access token
String encodedAuth = Base64.getEncoder().encodeToString((":" + ACCESS_TOKEN).getBytes());
connection.setRequestProperty("Authorization", "Basic " + encodedAuth);
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Print the response
System.out.println(response.toString());
} else {
System.out.println("GET request failed.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}