0

我一直在尝试在我的 android 应用程序中获取 Instamojo 链接以进行交易。为了测试,我一直在尝试发送发布请求以生成链接,但我一直收到 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();
}

}

4

1 回答 1

2

发送api_keyauth_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/

于 2016-02-13T06:55:19.383 回答