无法从 LH public api 生成访问令牌

Not able to generate access token from LH public api

我一直在尝试从 LH public API 生成访问令牌,但是当我使用 Volley 发送 POST 请求时,我得到了 "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);

        }
    });

}}

收到令牌后,我想创建一个应用程序来根据输入的坐标显示最近的机场。截至目前,那些纬度和经度变量与它无关。请帮忙。提前致谢。祝你今天过得愉快。

首先 我不认为 REST API 接受 JSON 对象作为请求数据。 如果您看到他们的文档,它会显示 "Content-Type: application/x-www-form-urlencoded"。 这意味着数据应该以 key1=value1&key2=value2...

的形式给出

以此作为发送x-www-form-uriencoded类请求的参考

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

在Map里面(我们用Map来设置Key=value类型的params)用这些来设置请求参数

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

希望这对您有所帮助。祝一切顺利