Heshsm Tl4s Heshsm Tl4s - 7 months ago 65
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:


unsupported_grant_type


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

finally I have found the solution :

replace :

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

With:

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