API does not support the grant type

I'm attempting to retrieve a token from the server response. It works perfectly fine using Postman, but when I try to debug it on Android, I encounter an error:

unsupported_grant_type

Below is my code snippet:

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 №1

After searching extensively, I have come across the solution :

Instead of :

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

Use:

post.setEntity(new StringEntity("grant_type=password&<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c393f293e222d2129717c7c7c7c0c2b212d2520622f2321">[email protected]</a>&password=00000", "UTF-8"));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to convert time measurements like minutes, hours, or days into seconds in React and then pass that information to an

Currently, I am working on an application that allows users to select a frequency unit (like seconds, minutes, hours, or days) and input the corresponding value. The challenge arises when I need to convert this value into seconds before sending it to the ...

Spring Jackson - Field "response" is not recognized and cannot be ignored within the context of the program

Trying to convert a jSon String to an Object using Jackson serialization. The jSon string that needs to be converted is: {"response":{"status":1,"count":"90120"}} Here's the Object structure: @JsonRootName("response") @JsonIgnoreProperties(ignoreU ...

Tips for sending a file to a Servlet using an Ajax request

When working with an HTML form in AEM that requires file attachments, the challenge arises when trying to send these files via a Java Servlet through a Rest API. The issue lies in transmitting file arrays through Ajax to the Java Servlet, as only string da ...

Creating a dynamic trio of graphs with HTML5, CSS3, and Vanilla JavaScript

I successfully created a tree graph using HTML5 and CSS3, but currently the nodes are static. I am looking to enhance the graph by making it dynamic. By dynamic, I mean that if the number of nodes increases or there are multiple children added, the graph ...

Converting a Kafka message to a Java object

I am currently working on converting a Kafka message received as a List of strings into a JSON object. Below is the snippet of code I am using: //Kafka consuming msg List<String> message = KafkaMessageConsumer.consumeMessage(props.getProperty(" ...

Oracle database not retaining Arabic characters

Our API is sending us Json payload, for example: { "customerName": "علي الدويش", "customerCode": "999999", "shipAddress1": "البلاغة", } To store this data, we are utilizing Sprin ...

Any tips on extracting a primitive data type from a JSON file?

The JSON file retrieved from a GET request is as follows: { "vcalendar": [ { "version": "2.0", "prodid": " CalendarID", "vevent": [ { "uid": " EventID", "dtstamp": "20180725T000000", ...

Android Studio encountered an unexpected object instead of an array at line 1, column 2 while attempting to execute a Retrofit request

Despite searching for a solution with Retrofit2 and following an online tutorial, I am still facing the same issue. The code worked fine in the tutorial but when I tried it on my own endpoint, I encountered the exception: java.lang.IllegalStateException: E ...

Break up JSON into distinct JSON files based on node value using Python

Looking to utilize Python to split a JSON file into separate files based on the "transactionTypeName" found within the transactions.details. Each file should include all details starting from careperson to username. Here is an example of the JSON file afte ...

AngularJS 500 server error

In my current project, I am developing a straightforward angularjs - J2EE application that fetches data from a mysql server and then displays it on an HTML page. The angular function is triggered on form submission as shown below: <div id="register_for ...

Difficulty encountered while transmitting the JSON object to third-party API

Initially, the API structure is beyond my control and all I can do is make a simple call to it. Here are the required parameters to pass in: { "ConsumerAggregatedAttributes": [ { "ConsumerAggregatedAttribut ...

Steps to upload an image using an API

I am attempting to use an API to upload a file to imgBB, but I encountered the following error message: {"status_code":400,"error":{"message":"Empty upload source.","code":130},"status_txt":&q ...

Extract JSON values based on a given condition

I am working on a Typescript project that involves an array and a JSON object. I need to extract the value of a property from the object based on another property's value being in the array. Here is the array: let country: string[] = [ 'AR' ...

What causes the Access-Control-Allow-Origin error to appear with AJAX requests but not with Rails?

After attempting to retrieve a response from the API of Career Builder using AJAX (trying xml, json, and jsonp), I consistently encountered the "Access-Control-Allow-Origin" error. However, when I followed icodeya's tutorial and retrieved a response f ...

What are the best ways to locate elements with changing IDs using Selenium?

I am looking to create a basic script that can generate an email account on the website: Every time I refresh the page, the input text fields have different IDs. How can I reliably locate these elements? And why would the ID be changed constantly? For in ...

Guide to accessing a mobile version website with Selenium WebDriver

I wrote a code snippet to open the mobile version of Facebook on my Desktop using Firefox by tweaking the user-agent. @Test public void fb() { FirefoxProfile ffprofile = new FirefoxProfile(); ffprofile.setPreference("general.useragent.over ...

Is there a way to determine if a specific JSONArray is contained within my existing JSONArray?

I am faced with the challenge of parsing a JSONArray that may or may not include an "attachmentPoint" element. Here is an example of the JSONArray structure: [{"entityClass":"DefaultEntityClass","mac":["86:2b:a2:f1:2b:9c"],"ipv4":["10.0.0.1"],"ipv6":[], ...

Determine the HTTP status code for a request using Vue.js and Ajax

I need to retrieve the HTTP status code after submitting a form (using the form submission function): return fetch(serviceUrl + 'Collect', { method: "POST", headers: new Headers({ "Content-Type": "application/json", Authoriza ...

exit the application immediately

Whenever I attempt to log in by clicking the login button on my application, it unexpectedly crashes. Can anyone help me understand why this is happening and provide a solution? Below is the code for my Login_Menu activity: package com.campuspro.start; ...

approaches for retrieving a specific JSON value associated with a key in Objective-C

I've recently started learning Obj-C and I'm struggling with getting the output of a JSON-Request. I think the issue lies in my understanding of arrays, dictionaries, and syntax. It would be really helpful if someone could guide me in the right d ...