0
I’m having a hard time integrating with an API using basic auth authentication.
I created a Java API using spring boot and spring security.
When I use Postman the request works without any problem, but when I try to perform the request via the APP code I receive a code denied 401.
String stringUrl = strings[0];
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        StringBuffer buffer = null;
        try {
            URL url = new URL(stringUrl);
            HttpURLConnection conexao = (HttpURLConnection) url.openConnection();
            conexao.setRequestProperty ("Authorization", authHeader);
            conexao.setConnectTimeout(20000);
            conexao.setReadTimeout(20000);
            conexao.setDoOutput(true);
            conexao.setRequestMethod("POST");
            conexao.setRequestProperty("grant_type", "password");
            conexao.setRequestProperty("username", "edimilson");
            conexao.setRequestProperty("password", "123");
            conexao.setRequestProperty("scope", "read,write");
            conexao.setRequestProperty("Content-Type", "application/json");
           // conexao.connect();
            int responseCode = conexao.getResponseCode();
            if (responseCode == 200){
                // Recupera os dados em Bytes
                inputStream = conexao.getInputStream();
                //inputStreamReader lê os dados em Bytes e decodifica para caracteres
                inputStreamReader = new InputStreamReader( inputStream );
                //Objeto utilizado para leitura dos caracteres do InpuStreamReader
                BufferedReader reader = new BufferedReader( inputStreamReader );
                buffer = new StringBuffer();
                String linha = "";
                while((linha = reader.readLine()) != null){
                    buffer.append( linha );
                }
            }else {
               // exibirMensagem("Houve um problema no envio da mensagem: " + responseCode);
                return "Houve um problema no envio da mensagem. \n" +
                        " " + responseCode;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.toString();
						


I found the problem. Simply my URL was not receiving the parameters. http://localhost:8080/oauth/token?... After fixing this problem worked normally
– Edi