Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在scala中调用poloniex交易api_Scala_Akka Http_Poloniex - Fatal编程技术网

如何在scala中调用poloniex交易api

如何在scala中调用poloniex交易api,scala,akka-http,poloniex,Scala,Akka Http,Poloniex,我的代码如下 import org.apache.commons.codec.binary.Hex import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.RawHeader import akka.http.scaladsl.model._ import akka.stream.ActorMaterializer import akka.util.B

我的代码如下

import org.apache.commons.codec.binary.Hex
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.headers.RawHeader
import akka.http.scaladsl.model._
import akka.stream.ActorMaterializer
import akka.util.ByteString
import javax.crypto.spec.SecretKeySpec
import javax.crypto.Mac

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class Poloniex {
  def post(postData: String): Future[HttpResponse] = {
    val command = "returnBalances"
    val nonce = (System.currentTimeMillis / 1000).toString
    val postData = s"nonce=$nonce&command=$command"
    val headers: scala.collection.immutable.Seq[HttpHeader] = scala.collection.immutable.Seq(
      RawHeader("Key", API_KEY),
      RawHeader("Sign", hmacSha512(postData))
    )

    val entity = HttpEntity(postData)

    for {
      r <- Http().singleRequest(HttpRequest(HttpMethods.POST, TRADING_URL, headers, entity))
    } yield r
  }

  private def hmacSha512(postData: String): String = {
    val secret = new SecretKeySpec(API_SECRET.getBytes, HMAC_SHA512)
    val mac = Mac.getInstance(HMAC_SHA512)
    mac.init(secret)
    val result: Array[Byte] = mac.doFinal(postData.getBytes)
    new String(Hex.encodeHex(result))
  }
}
但我找不到为什么我会出错。 你能找到它出错的原因吗

api文档在这里。


谢谢。

请求的内容类型必须是
application/x-www-form-urlencoded
。要设置此选项,请使用:

{"error":"Invalid command."}
val entity =
  FormData(Map("nonce" -> nonce, "command" -> command)).toEntity(HttpCharsets.`UTF-8`)