0

我的代码如下。

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

我明白了

{"error":"Invalid command."}

但我找不到为什么会出错。你能找到它出错的原因吗?

api 文档在这里。 https://poloniex.com/support/api/

谢谢。

4

1 回答 1

1

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

val entity =
  FormData(Map("nonce" -> nonce, "command" -> command)).toEntity(HttpCharsets.`UTF-8`)
于 2017-08-18T19:51:17.073 回答