The app needs to connect to a API that only support OAuth2 Resource Owner Password Credentials Grant. I tryed using the code bellow, but get the response code 400 "bad request". Using the same code I can connect to a normal site and retrive the content.
I know that the API code is working because using Postman works. In Postman I just make a post request, suppling the username, password, and grant_type, and using x-www-form-urlencoded
The return of the connection is a json.
Any ideas what it's wrong? Should I use a 3rd party library? Any recomendations? Thanks.
In the code I changed the credentials and the API link
public class GetData extends AsyncTask<String, String, String> {
@Override
public String doInBackground(String... args) {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://someaddress.azurewebsites.net/api/token");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("username", "[email protected]");
urlConnection.setRequestProperty("password", "123");
urlConnection.setRequestProperty("grant_type", "password");
urlConnection.connect();
int responseCode = urlConnection.getResponseCode();
String responseMsg = urlConnection.getResponseMessage();
if (responseCode >= 400 && responseCode <= 499) {
throw new Exception(responseMsg + " :: " + responseCode);
}
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
System.out.print(current);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
//Do something with the JSON string
}
}
Adding some log, as sugested by Amod. This is the return of the printStackTrace in the code above.
12-12 01:16:33.210 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: java.lang.Exception: Bad Request :: 400
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: at com.marcussabino.tsftestedeconexo.GetData$override.doInBackground(GetData.java:40)
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: at com.marcussabino.tsftestedeconexo.GetData$override.access$dispatch(GetData.java)
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: at com.marcussabino.tsftestedeconexo.GetData.doInBackground(GetData.java:0)
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: at com.marcussabino.tsftestedeconexo.GetData.doInBackground(GetData.java:15)
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: at java.lang.Thread.run(Thread.java:818)