0

任何人都可以帮助我解决我面临的这个错误吗?我对java相当陌生。在这里,我试图在 Duo Admin API 上创建一个用户,但是当我运行这段代码时,我得到了这个

    import java.io.IOException;
    import java.net.URI;
    import java.net.http.HttpClient;
    import java.net.http.HttpRequest;
    import java.net.http.HttpResponse;
    import java.util.Base64;
    
    // HTTP basic authentication example in Java using the RTC Server RESTful API
    public class Untitled {
    
        public static void main(String[] args) throws IOException, InterruptedException {
    
            // Customer ID
            final String customerKey = "key";
            // Customer secret
            final String customerSecret = "secret";
    
            // Concatenate customer key and customer secret and use base64 to encode the
            // concatenated string
            String plainCredentials = customerKey + ":" + customerSecret;
            String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
            // Create authorization header
            String authorizationHeader = "Basic " + base64Credentials;
    
            HttpClient client = HttpClient.newHttpClient();
    
            // Create HTTP request object
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create("https://api-e9770554.duosecurity.com/admin/v1/users")).GET()
                    .header("Authorization", authorizationHeader).header("Content-Type", "application/json").build();
            // Send HTTP request
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
    
            System.out.println(response.body());
        }
    }

错误

{"code": 40104, "message": "Missing request timestamp", "stat": "FAIL"}
4

1 回答 1

1

这与 Java 无关,而是与 duosecurity.com API 有关。显然缺少一些必需的数据(请求时间戳)。

Duo 帮助页面指出:

40104 Missing request timestamp
解释:Date 或 X-Duo-Date 标头丢失或格式不正确。
解决方案:确保 Date 或 X-Duo-Date 标头存在且格式正确。

所以你应该添加类似的东西

.header("X-Duo-Date", "Tue, 17 Aug 2021 12:24:26 -0000") 

(注:日期为 RFC 2822 格式)

于 2021-08-17T11:20:26.757 回答