-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestGeminiKeys.java
More file actions
160 lines (138 loc) · 6.92 KB
/
TestGeminiKeys.java
File metadata and controls
160 lines (138 loc) · 6.92 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class TestGeminiKeys {
private static final String API_BASE = "https://generativelanguage.googleapis.com/v1/models/";
private static final Gson gson = new Gson();
// Test keys (will not be committed)
// Models to test (actual available models from API)
private static final String[] TEST_MODELS = {
"gemini-2.0-flash-lite", // Smallest/fastest
"gemini-2.0-flash-lite-001", // Alternative lite
"gemini-2.0-flash", // Standard
"gemini-2.5-flash-lite", // Newer lite
"gemini-2.5-flash" // Newer standard (avoid 2.5-pro as requested)
};
public static void main(String[] args) {
System.out.println("Testing " + TEST_KEYS.length + " Gemini API keys with " + TEST_MODELS.length + " models...");
System.out.println();
int workingCount = 0;
for (int i = 0; i < TEST_KEYS.length; i++) {
String key = TEST_KEYS[i];
String keyLabel = "Key " + (i + 1) + " (...)" + key.substring(key.length() - 4);
System.out.println("========================================");
System.out.println("Testing: " + keyLabel);
System.out.println("========================================");
boolean keyWorks = false;
String workingModel = null;
for (String model : TEST_MODELS) {
System.out.print(" Testing model: " + model + " ... ");
try {
String response = testModel(key, model);
if (response != null && !response.isEmpty()) {
System.out.println("✓ WORKS!");
System.out.println(" Response: " + response.substring(0, Math.min(50, response.length())) + "...");
keyWorks = true;
workingModel = model;
break; // Found a working model, move to next key
} else {
System.out.println("✗ No response");
}
} catch (Exception e) {
System.out.println("✗ FAILED: " + e.getMessage());
}
}
if (keyWorks) {
System.out.println();
System.out.println(" ✓✓✓ KEY WORKS with model: " + workingModel);
workingCount++;
} else {
System.out.println();
System.out.println(" ✗✗✗ KEY FAILED - All models returned errors");
}
System.out.println();
}
System.out.println("========================================");
System.out.println("SUMMARY");
System.out.println("========================================");
System.out.println("Working keys: " + workingCount + " / " + TEST_KEYS.length);
System.out.println();
if (workingCount > 0) {
System.out.println("✓ You have working Gemini API keys!");
System.out.println(" These can be added to your .env.SECURE file");
} else {
System.out.println("✗ No working Gemini API keys found");
System.out.println(" All keys may be blocked or quota exceeded");
}
}
private static String testModel(String apiKey, String model) throws Exception {
String urlString = API_BASE + model + ":generateContent?key=" + apiKey;
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setConnectTimeout(5000);
conn.setReadTimeout(10000);
// Build simple test request
JsonObject requestBody = new JsonObject();
JsonArray contents = new JsonArray();
JsonObject content = new JsonObject();
JsonArray parts = new JsonArray();
JsonObject part = new JsonObject();
part.addProperty("text", "Say 'test successful' in 3 words");
parts.add(part);
content.add("parts", parts);
contents.add(content);
requestBody.add("contents", contents);
// Send request
try (OutputStream os = conn.getOutputStream()) {
byte[] input = requestBody.toString().getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
// Read response
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
// Parse response
JsonObject jsonResponse = gson.fromJson(response.toString(), JsonObject.class);
if (jsonResponse.has("candidates")) {
JsonArray candidates = jsonResponse.getAsJsonArray("candidates");
if (candidates.size() > 0) {
JsonObject candidate = candidates.get(0).getAsJsonObject();
JsonObject contentObj = candidate.getAsJsonObject("content");
JsonArray partsArray = contentObj.getAsJsonArray("parts");
if (partsArray.size() > 0) {
return partsArray.get(0).getAsJsonObject().get("text").getAsString();
}
}
}
return null;
}
} else {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getErrorStream(), StandardCharsets.UTF_8))) {
StringBuilder errorResponse = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
errorResponse.append(responseLine.trim());
}
throw new Exception("HTTP " + responseCode + ": " + errorResponse.toString());
}
}
} finally {
conn.disconnect();
}
}
}