首页 > 解决方案 > Not able to generate access token from LH public api

问题描述

I had been trying to generate access token from LH public API but when I am using Volley to send POST request I am getting "com.android.Volley.AuthFailureError".

package jvtech.jasvir;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

EditText latitude,longitude;
Button submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    latitude=(EditText)findViewById(R.id.lat);
    longitude=(EditText)findViewById(R.id.lon);
    submit=(Button)findViewById(R.id.submit);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String url="https://api.lufthansa.com/v1/oauth/token";
            String key="abc";
            String secret="abc";
            String grant_type=  "abc";

            JSONObject object=new JSONObject();
            try{
                object.put("client_id",key);
                object.put("client_secret",secret);
                object.put("grant_type",grant_type);
            }
            catch(JSONException e)
            {
                e.printStackTrace();
            }

            RequestQueue queue= Volley.newRequestQueue(MainActivity.this);

            JsonObjectRequest request=new JsonObjectRequest(Request.Method.POST, url, object, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try
                    {
                        Toast.makeText(MainActivity.this, ""+response.getString("access_token"), Toast.LENGTH_SHORT).show();
                    }
                    catch (JSONException e)
                    {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, ""+error, Toast.LENGTH_SHORT).show();
                }
            });

            queue.add(request);

        }
    });

}}

After receiving the token I want to create an application to display the closest airports according to the coordinates entered. As of now those latitude and longitude variables have nothing to do with it. Please help. Thanks in advance. Have a nice day.

标签: javaandroidpostandroid-volley

解决方案


First off I don't think that REST API is accepting a JSON object as the request data. If you see their documentation it says "Content-Type: application/x-www-form-urlencoded". Which means that the data should be given in the form of key1=value1&key2=value2...

Take this as a reference for sending a x-www-form-uriencoded type of request

https://www.itsalif.info/content/android-volley-tutorial-http-get-post-put#highlighter_784982

and inside the Map (we use Map to set the Key=value type of params) use these to set the request parameters

param.put("client_id","your client id");
param.put("client_secret", "your secret");
param.put("grant_type","client_credentials");

Hope this was helpful. All the best


推荐阅读