在使用android
volley 进行 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));
}
}
请帮助我...