Venugopal
Venugopal

Reputation: 1

Server returned HTTP response code: 400 for oauth token generation in java httpurlconnection

Tried to generate access token using oauth grantType password.But getting java.io.IOException: Server returned HTTP response code: 400 for URL: https://Host/nidp/oauth/nam/token. please find the below code i tried

String parameter = "grantType=password&clientID=<clientid>&clientSecret=<client secret>&username=xyz1233&password=qweteyeueu&scope=openid arca"
    byte[] data = parameter.getBytes("UTF-8");
    URL obj = new URL(url);
    connection = (HttpURLConnection) obj.openConnection();
    connection.setDoOutput(true);
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("Content-Length", String.valueOf(data.length));
    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());

    wr.write(data);
    wr.flush();
    wr.close();

    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer response = new StringBuffer();
    while ((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
    }
    rd.close();

Please help me to resolve this.

Upvotes: 0

Views: 515

Answers (1)

KLGR
KLGR

Reputation: 31

Are you sure, that you emplace "clientid", "client secret" and the actual address into request? Are you separate address and method with the '?' symbol? As long as i remember, you cant add spaces in request like in "scope=openid arca"

Upvotes: 1

Related Questions