简体   繁体   中英

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM