Get web toi api / asp from java / android

0

Hello wrote a web api in C # to authenticate, using postman sent the parameters and returns the token correctly, I will leave at the end of post the parameters for testing, and searched for a number of code to bring parameters in JSON from an web api always send me back the Código 400 Bad request , I just want to connect from < strong> Java using the user and the password so that the server returns the token generated for that login I used many code and still nothing, this is the token that I can generate using the postman but from Java I can only generate 400 bad request .

I'm trying to authenticate to use it in an app for Android

{
  "access_token": "...",
  "token_type": "bearer",
  "expires_in": 86399
}
url token: http://pedidostf.azurewebsites.net/token  
username : admin  
password : *****
grant_type : ********

This would be the last method I'm using for the connection

private class POST extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        //     InputStream inputStream = null;
        String cliente = params[0];
        String clave = params[1];
        String json = "";
        try {

            OkHttpClient client = new OkHttpClient();
            Request.Builder builder = new Request.Builder();
            builder.url(AppConfig.URL_TOKEN);
            builder.addHeader("Content-Type", "application/x-www-form-urlencoded");


            FormBody.Builder parameters = new FormBody.Builder();
            parameters.add("grant_type", "password");
            parameters.add("username", cliente);
            parameters.add("password", clave);
            builder.post(parameters.build());

            Response response = client.newCall(builder.build()).execute();
            if (response.isSuccessful()) {
                 json = response.body().string();
                System.out.println("CONTENIDO::  " + json);

            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error: " + e);
        }
    return json;
}

I got it here

link

    
asked by Heisenberg06 05.01.2017 в 17:04
source

0 answers