I am using Duo Security Admin API. But when I call the api using below code it shows:
Get Users request failed with HTTP (response code: 401)
Here is my java code:
void users(){
// Duo Admin API credentials and API hostname
String integrationKey = "DI7ABPU9TUJQO14RET9Q";
String secretKey = "YzDs7ZeQGMllravxDQxcn4jNAwyqF42P1XBDdGd2";
String apiHostname = "api-d221a358.duosecurity.com";
try {
String usersUrl = "https://" + apiHostname + "/admin/v1/users";
// Create URL object
URL url = new URL(usersUrl);
// Open a connection to the URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Set request method to GET
connection.setRequestMethod("GET");
// Set request headers
connection.setRequestProperty("Authorization", "Basic " + getBase64Credentials(integrationKey, secretKey));
connection.setRequestProperty("Date", OffsetDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME));
connection.setRequestProperty("Content-Type", "application/json");
// Get the HTTP response code
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
// Read and print the response (list of users)
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
System.out.println("List of Users: " + response.toString());
}
} else {
System.out.println("Get Users request failed with HTTP response code: " + responseCode);
}
// Close the connection
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
// Encode credentials as Base64
private static String getBase64Credentials(String integrationKey, String secretKey) {
String credentials = integrationKey + ":" + secretKey;
return java.util.Base64.getEncoder().encodeToString(credentials.getBytes());
}
What is the mistake i have done with my code? Is it right way to call Duo Security Admin API?
Please help me..
For get user Duo admin API code that works for me is
The generate signature code is
get users service class is
The main class is