错误" BasicNetwork.performRequest:https://poloniex.com/tradingApi&#34的意外响应代码403;

时间:2018-06-04 08:31:33

标签: android api android-volley poloniex

使用android凌空进行poloniex交易api时,我收到了错误

  

" BasicNetwork.performRequest:意外的响应代码403 for   https://poloniex.com/tradingApi"

我在网上搜索解决方案,没有任何帮助我修复此错误。我使用了正确的api密钥和密钥没有任何错误。

以下是我的代码:

public class MainActivity extends AppCompatActivity {
    String nonce;
    String apiKey;
    String secretKey;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        RequestQueue queue = SingletonRequestQueue.getInstance(getApplicationContext()).getRequestQueue();
        VolleyLog.DEBUG = true;
        String uri =  "https://poloniex.com/tradingApi";
        final String nonce=String.valueOf(System.currentTimeMillis());

        Map<String, String> jsonParams = new HashMap<String, String>();
        jsonParams.put("nonce",nonce);
        jsonParams.put("command", "returnBalances");

        final JsonObjectRequest myRequest = new JsonObjectRequest(
                Request.Method.POST,
                uri,
                new JSONObject(jsonParams),

                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        VolleyLog.wtf(response.toString(), "utf-8");
                        Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Error from Response",error.toString());
                    }
                }) {

            @Override
            public Map<String, String> getHeaders()  {
                HashMap<String, String> headers=null;
                try {
                    String apiKey = "MY API KEY";

                    Mac mac = null;
                    String secretKey = "MY SECRET KEY";
                    SecretKeySpec keyspec = null;
                    keyspec = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA512");
                    mac = Mac.getInstance("HmacSHA512");
                    mac.init(keyspec);
                    Map<String, String> args = new HashMap<String, String>();
                    args.put("nonce", nonce);
                    args.put("command", "returnBalances");
                    String postData = "";
                    for (Iterator<String> iter = args.keySet().iterator(); iter.hasNext(); ) {
                        String arg = iter.next();
                        if (postData.length() > 0) {
                            postData += "&";
                        }
                        try {
                            postData += arg + "=" + URLEncoder.encode(args.get(arg), "UTF-8");
                        } catch (Exception e) {

                        }
                    }

                    headers = new HashMap<String, String>();
                    headers.put("Key", apiKey);
                    headers.put("Sign", toHex(mac.doFinal(postData.getBytes("UTF-8"))));
                    headers.put("Content-Type", "application/json; charset=utf-8");
                    headers.put("User-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36");
                }catch (Exception e) {

                }
                return headers;
            }
        };

        myRequest.setRetryPolicy(new DefaultRetryPolicy(0,-1,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(myRequest);

    }

   private String toHex(byte[] b) throws UnsupportedEncodingException {
        return String.format("%040x", new BigInteger(1,b));
    }
}

请帮助我......

1 个答案:

答案 0 :(得分:0)

发布请求不是json,而是带有数据的发布,因此您必须先删除:

"Content-Type", "application/json; charset=utf-8");
"User-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36");

注意:如果添加这些标头,则API将返回“无效命令”。


如果您希望它可以工作,它必须看起来像:

  • 发布网址:https://poloniex.com/tradingApi

  • 数据:{'nonce': 1529928989092, 'command': 'returnBalances'} 参数需要作为数据而不是参数发送

  • 标题:{'Key': 'yourkeyhere', 'Sign': 'yoursignhere'}

什么都没有。