2017-12-16 1 views
0

ここで自分のコードを調べてください。 Bitfinex APIからエラーが戻ってきました: スレッド "main"の例外java.util.MissingResourceException:サーバーがHTTP応答コードを返しました:URL:https://api.bitfinex.com/v1/account_infos/ - > {"message": "指定する必要がありますURLのパスと一致するペイロード内の要求 'フィールド "}BitfinexペイロードエラーV1 Java

以下

私のコード:。

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(); 
    } 
} 

答えて

0

これら二つが反転している:彼らは反対である必要があり

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

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

こんにちは。答えをありがとう、私は変更しましたが、まだ仕事をdoest ..同じエラー。私が間違っていることを確認するのに役立つでしょう –

+0

認証手順は次のとおりです: ペイロードはパラメータオブジェクトで、最初にJSONエンコードされ、Base64にエンコードされます。 payload = parameters-object - > JSON encode - > base64 署名は、メッセージがペイロードで、HMAC-SHA384ハッシュの16進ダイジェストであり、秘密鍵はAPIの秘密です。 署名= HMAC-SHA384(ペイロード、API-秘密).digest( '進') 送信(APIキー、ペイロード、署名) これらはHTTPヘッダ名前として符号化される。 X-BFX-APIKEY X-BFX-PAYLOAD X-BFX-SIGNATURE –

関連する問題