-1

在使用androidvolley 进行 poloniex 交易 api 时出现错误

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

我已经在网上搜索了解决方案,但没有什么能帮助我解决这个错误。我使用了正确的 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));
    }
}

请帮助我...

4

1 回答 1

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'}

没有更多或更少。

于 2018-06-25T12:23:51.940 回答