How can I add Authorization and Date/time header in the below code?

I am creating a Java Rest api to create users on Google Duo admin. I am following the documentation https://duo.com/docs/adminapi and they need auth and date/time header. I am fairly new to java can anyone guide me?

This is the updated code which I with authentication and date/time header but now getting the below error.

UPDATED CODE

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;


public class DuoAdminAPI {
    public static void POSTRequest() throws IOException {



        String userCredentials = "Username:Password";
        String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));

        String dateTime = OffsetDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME); //RFC_1123 == RFC_2822


        final String POST_PARAMS = "{n" + ""userId": 101,rn" +
                "    "id": 101,rn" +
                "    "title": "Test Title",rn" +
                "    "body": "Test Body"" + "n}";
        System.out.println(POST_PARAMS);
        URL obj = new URL("https://api-e9770554.duosecurity.com");
        HttpURLConnection postConnection = (HttpURLConnection) obj.openConnection();
        postConnection.setRequestMethod("POST");
        postConnection.setRequestProperty("Content-Type", "application/json");
        postConnection.setRequestProperty("Authorization", basicAuth);

        postConnection.setRequestProperty("Date", dateTime);



        postConnection.setDoOutput(true);
        OutputStream os = postConnection.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();


        int responseCode = postConnection.getResponseCode();
        System.out.println("POST Response Code :  " + responseCode);
        System.out.println("POST Response Message : " + postConnection.getResponseMessage());

        if (responseCode == HttpURLConnection.HTTP_CREATED) { //success
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    postConnection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result
            System.out.println(response.toString());
        } else {
            System.out.println("POST NOT WORKED");
        }
    }
}

ERROR

{
  "code": 40101,
  "message": "Missing request credentials",
  "stat": "FAIL"
}



 Response code: 401 (Unauthorized); Time: 2022ms; Content    length: 73 bytes

Answer

As I have read the documentation, the authorization is Basic:

String userCredentials = "username:password";
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));

For date is this:

String dateTime = OffsetDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME); //RFC_1123 == RFC_2822

In their documentation they require RFC 2822 format. We already have declared RFC 1123 in DateTimeFormatter, which practically it is the same.

And this is the way you can add them in headers:

postConnection.setRequestProperty("Authorization", basicAuth);
postConnection.setRequestProperty("Date", dateTime);