提问人:Enamul Haque 提问时间:9/21/2023 更新时间:9/21/2023 访问量:16
Duo Security Admin API 用户请求在 Java 中使用失败
Duo Security Admin API users request failed using in Java
问:
我正在使用 Duo Security Admin API。但是当我使用以下代码调用 api 时,它显示:
Get Users request failed with HTTP (response code: 401)
这是我的 java 代码:
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());
}
我的代码犯了什么错误? 调用 Duo Security Admin API 的方法是否正确?
请帮帮我..
答: 暂无答案
评论