Bitfinex有效负载错误V1 Java

问题描述 投票:0回答:1

你能不能试着在这里查看我的代码。我从Bitfinex Api收到错误:线程“main”中的异常java.util.MissingResourceException:找不到服务器返回的HTTP响应代码:400为URL:https://api.bitfinex.com/v1/account_infos/ - > {“message”:“必须指定'请求'有效负载中与URL路径匹配的字段。“}

我的代码如下:

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONObject;

import java.util.Base64;
import java.util.Base64.Encoder;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;

public class ExecutorBitfin {
    private static String APIKEY = "xx";
    private static String APISECRET = "xx";
    private static final String ALGORITHM_HMACSHA384 = "HmacSHA384";
    private static final String TAG = ExecutorBitfin.class.getSimpleName();

    private String getNonce() {
        return "1" + System.currentTimeMillis();
    }

    public ExecutorBitfin(String APIKEY, String APISECRET) {
        this.APIKEY = APIKEY;
        this.APISECRET = APISECRET;
    }

    @SuppressWarnings("unchecked")

    public String executaPostBalance() throws IOException {

        String sResponse;
        HttpURLConnection con = null;
        String PATH = "/v1/account_infos/";
        String method = "POST";
        try {
            URL url1 = new URL("https://api.bitfinex.com" + PATH);
            con = (HttpsURLConnection) url1.openConnection();
            con.setRequestMethod(method);

            // send post
            con.setDoOutput(true);
            con.setDoInput(true);

            JSONObject jo = new JSONObject();

            jo.put(PATH, "request");
            jo.put(getNonce(), "nonce");

            String payload = jo.toString();

            String payload_base64 = Base64.getEncoder().encodeToString(payload.getBytes());

            String payload_sha384hmac = HmacDigest(payload_base64, APISECRET, ALGORITHM_HMACSHA384);

            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("Accept", "application/json");
            con.addRequestProperty("X-BFX-APIKEY", APIKEY);
            con.addRequestProperty("X-BFX-PAYLOAD", payload_base64);
            con.addRequestProperty("X-BFX-SIGNATURE", payload_sha384hmac);

            InputStream in = new BufferedInputStream(con.getInputStream());
            return convertStreamToString(in);

        } catch (MalformedURLException e) {
            throw new IOException(e.getClass().getName(), e);
        } catch (ProtocolException e) {
            throw new IOException(e.getClass().getName(), e);
        } catch (IOException e) {

            String errMsg = e.getLocalizedMessage();

            if (con != null) {
                try {
                    sResponse = convertStreamToString(con.getErrorStream());
                    errMsg += " -> " + sResponse;
                    Logger.getLogger(TAG, errMsg);
                    return sResponse;
                } catch (IOException e1) {
                    errMsg += " Error on reading error-stream. -> " + e1.getLocalizedMessage();
                    Logger.getLogger(TAG, errMsg);
                    throw new IOException(e.getClass().getName(), e1);
                }
            } else {
                throw new IOException(e.getClass().getName(), e);
            }
        } catch (JSONException e) {
            String msg = "Error on setting up the connection to server";
            throw new IOException(msg, e);
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }
    }

    private String convertStreamToString(InputStream is) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('\n');
            }
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    @SuppressWarnings("finally")
    private String HmacDigest(String msg, String keyString, String algo) {
        String digest = null;
        try {
            SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);
            Mac mac = Mac.getInstance(algo);
            mac.init(key);

            byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));

            StringBuffer hash = new StringBuffer();
            for (int i = 0; i < bytes.length; i++) {
                String hex = Integer.toHexString(0xFF & bytes[i]);
                if (hex.length() == 1) {
                    hash.append('0');
                }
                hash.append(hex);
            }
            digest = hash.toString();
        } catch (UnsupportedEncodingException e) {
            Logger.getLogger(TAG, "Exception: " + e.getMessage());
        } catch (InvalidKeyException e) {
            Logger.getLogger(TAG, "Exception: " + e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            Logger.getLogger(TAG, "Exception: " + e.getMessage());
        }
        return digest;
    }

    public static void main(String[] args) throws IOException {
        new ExecutorBitfin(APIKEY, APISECRET).executaPostBalance();
    }
}
java api
1个回答
0
投票

这两个被翻转:

jo.put(PATH, "request");
jo.put(getNonce(), "nonce");

他们应该是相反的:

jo.put("request", PATH);
jo.put("nonce", getNonce());
© www.soinside.com 2019 - 2024. All rights reserved.