DUO API Error: 'Missing request timestamp' (Code 40104) - Help needed

I’m a Java beginner trying to use the Duo Admin API to create a user. When I run my code, I get an error saying ‘Missing request timestamp’ with code 40104. Can someone help me figure out what’s wrong?

Here’s a simplified version of what I’m trying to do:

import java.net.http.*;
import java.util.Base64;

public class DuoApiTest {
    public static void main(String[] args) throws Exception {
        String apiKey = "mykey";
        String apiSecret = "mysecret";
        String credentials = Base64.getEncoder().encodeToString((apiKey + ":" + apiSecret).getBytes());

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
            .uri(new URI("https://api.duosecurity.com/admin/v1/users"))
            .header("Authorization", "Basic " + credentials)
            .header("Content-Type", "application/json")
            .GET()
            .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

The response I get is:

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

What am I missing? Do I need to add a timestamp to my request? Any help would be appreciated!

oh yeah, i ran into this same issue. you need to add a ‘Date’ header to your request. it’s a bit tricky, but you gotta format it like RFC 2822. try something like this:

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateHeader = dateFormat.format(new Date());

ten just add .header(“Date”, dateHeader) to your request builder. that should fix it!

Hey there, I’ve dealt with this Duo API issue before. The problem is that you’re missing a crucial piece in your request - the timestamp. Duo uses this for security reasons.

Here’s what you need to do:

First, import these:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

Then, before you build your request, add this code:

SimpleDateFormat dateFormat = new SimpleDateFormat(“EEE, dd MMM yyyy HH:mm:ss Z”);
dateFormat.setTimeZone(TimeZone.getTimeZone(“UTC”));
String dateHeader = dateFormat.format(new Date());

Finally, add this to your request builder:
.header(“Date”, dateHeader)

This should solve your ‘Missing request timestamp’ error. The Duo API is pretty strict about this timestamp thing, so make sure it’s in the right format (RFC 2822).

Also, a word of advice - always check the API documentation thoroughly. Sometimes these little details can be easy to miss but cause big headaches. Good luck with your project!

The error you’re encountering is due to a missing timestamp in your request. Duo’s API requires a ‘Date’ header for authentication purposes. Here’s how you can modify your code to include it:

Import these additional classes:
java.text.SimpleDateFormat
java.util.Date
java.util.TimeZone

Before creating your HttpRequest, add the following code to generate the date header:

SimpleDateFormat dateFormat = new SimpleDateFormat(“EEE, dd MMM yyyy HH:mm:ss Z”);
dateFormat.setTimeZone(TimeZone.getTimeZone(“UTC”));
String dateHeader = dateFormat.format(new Date());

Then include the header in your request builder:
.header(“Date”, dateHeader)

This should resolve the ‘Missing request timestamp’ error. Remember to handle any potential exceptions when working with dates and time zones.