Error while performing Basic Auth authentication in REST API on android

Asked

Viewed 146 times

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.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

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();

inserir a descrição da imagem aqui

1 answer

0

try so on that line:

conexao.setRequestProperty("scope", "read%20write");
  • I found the problem. Simply my URL was not receiving the parameters. http://localhost:8080/oauth/token?... After fixing this problem worked normally

Browser other questions tagged

You are not signed in. Login or sign up in order to post.