java - 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.
解决方案
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
推荐阅读
- r - 仅当在闪亮的应用程序中按下特定的 actionButton 时才显示侧边栏
- python - TclError:无法识别图像文件“black.gif”中的数据我不明白为什么即使文件类型是 GIF 也会出现错误
- c++ - 您如何在 C 预处理器中使用定义进行逻辑异或
- git - 远程“原点”不支持 LFS 锁定 API //尝试使用 lfs 在 github 中上传超过 100mb 的 model.h5 文件
- scala - Scala SBT 编译挂起(SBT 1.5.0、Scala 2.13.5、Java 11.0.10)
- flutter - 使用 Flutter 从 API 接收数据
- python - 如何将嵌套列表(3D)转换为数组
- sql - SQL Developer 在我创建表时强制大写
- excel - 将路径附加到用户表单选择
- java - ClassCastException 日期范围[]