Heshsm Tl4s Heshsm Tl4s - 1 year ago 150
Java Question

Unsupported grant type with API

I'm trying to get token from sever response it works great with postMan , but when debug it with android it gets error:


here is my code:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(LoginURL);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
// post.setHeader("Accept", "application/x-www-form-urlencoded");
JSONObject obj = new JSONObject();
try {
obj.put("grant_type", "password");
obj.put("password", PasswordEditText);
obj.put("username", EmailEditText+"gfg");

post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
results = EntityUtils.toString(entity);
myObject = new JSONObject(results);

Answer Source

finally I have found the solution :

replace :

  post.setEntity(new StringEntity(obj.toString(), "UTF-8"));


                post.setEntity(new StringEntity("grant_type=password&username=0000@gmail.com&password=00000", "UTF-8"));