我一直在尝试在我的Android应用程序中获取Instamojo链接以进行交易。为了测试我一直在尝试发送post请求以生成链接但是我一直在获取AuthFailureError。我在下面发布我的代码。我不知道我的代码中是否存在某些错误,或者我是否按照错误的方式将Instamojo集成到我的应用程序中。请帮忙。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private RequestQueue mQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mQueue = CustomVolleyRequestQueue.getInstance(this)
.getRequestQueue();
String url = "https://www.instamojo.com/api/1.1/payment-requests/";
JSONObject params = new JSONObject();
try {
params.put("purpose","selling");
params.put("amount","20");
} catch (JSONException e) {
e.printStackTrace();
}
final CustomJSONObjectRequest jsonRequest=new CustomJSONObjectRequest(Request.Method.POST, url,params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
Log.d("animesh",response.toString());
}
},new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(MainActivity.this, "no internet connection", Toast.LENGTH_SHORT).show();
Log.d("animesh", error.toString());
}
});
mQueue.add(jsonRequest);
} }
CustomJSONObjectRequest.java:
公共类CustomJSONObjectRequest扩展了JsonObjectRequest {
public CustomJSONObjectRequest(int method, String url, JSONObject jsonRequest,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
super(method, url, jsonRequest, listener, errorListener);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
//String creds = String.format("%s:%s","archerpenny_glide","archer@#62@glide*");
//String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
headers.put("api_key", "**********************************");
headers.put("auth_token", "*******************************");
return headers;
}
@Override
public RetryPolicy getRetryPolicy() {
// here you can write a custom retry policy
return super.getRetryPolicy();
}
}
答案 0 :(得分:2)
要发送您正在执行的api_key
和auth_token
:
headers.put("api_key", "**********************************");
headers.put("auth_token", "*******************************");
相反,你需要这样做:
headers.put("X-Api-Key", "**********************************");
headers.put("X-Auth-Token", "*******************************");
您可以在此处参考REST API
文档:https://www.instamojo.com/developers/rest/