Authentication Service to GetToken return "You have to pass in both email and password to retrieve your token." using Java HTTPPost

Hi there,

I am trying to write a java app to get the token, however I keep getting the following error:

  • You have to pass in both email and password to retrieve your token.

Below is a screenshot of my codes:

Appreciate if there is any who can help/advice.

@wjfoong Have you try out using PostMan first before you implement into your Java code? You also can try out our Swagger where it also supports Client SDK (Under Export) such as Java to call our OneMap APIs: https://developers.onemap.sg/swagger

Sorry for the late reply.

I try the postman and it works. But somehow, i can’t get it to work in the piece of java codes shown.

@wjfoong, maybe you can consult from the public community such as stackoverflow.com, since this is not really related to OneMap as the OneMap Swagger and the Postman are working.

Here’s the example for POST Method in Java via stackoverflow (but change to what you need):

import java.io.*;
import java.net.*;
import java.util.*;

class Test {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://example.net/new-message.php");
        Map<String,Object> params = new LinkedHashMap<>();
        params.put("name", "John Doe");
        params.put("email", "johndoe@example.com");
        params.put("reply_to_thread", 10394);
        params.put("message", "Shark attacks in Botany Bay have gotten out of control. We need more defensive dolphins to protect the schools here, but Mayor Porpoise is too busy stuffing his snout with lobsters. He's so shellfish.");

        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String,Object> param : params.entrySet()) {
            if (postData.length() != 0) postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }
        byte[] postDataBytes = postData.toString().getBytes("UTF-8");

        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        conn.setDoOutput(true);
        conn.getOutputStream().write(postDataBytes);

        Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

        for (int c; (c = in.read()) >= 0;)
            System.out.print((char)c);
    }
}