0

我想做的是调用 Coinbase 沙盒 API 来获取配置文件的所有帐户。
我正在关注官方文档https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getaccounts
但不断收到此错误
Client request(https://api-public.sandbox.exchange.coinbase.com/accounts) invalid: 401 Unauthorized. Text: "{"message":"invalid signature"}"

我错过了什么吗?

suspend fun getTradingAccounts(): String {
        val timestamp = getTimeStamp()
        val response: String = client.get("https://api-public.sandbox.exchange.coinbase.com/accounts") {
            headers {
                append("Accept", "application/json")
                append("Content-Type", "application/json")
                append(
                    "cb-access-key",
                    "MY_KEY..."
                )
                append("cb-access-passphrase", "MY_PASSPHRASE....")
                append("cb-access-sign", signMessage(
                    timestamp = timestamp,
                    method = "GET",
                    path = "https://api-public.sandbox.exchange.coinbase.com/accounts"
                ))
                append("cb-access-timestamp", timestamp)
            }
        }
        return response
    }

    private fun getTimeStamp(): String {
        val  time = LocalDateTime.now()
        val zoneId = ZoneId.of("Europe/London")
        val  epoch = time.atZone(zoneId).toEpochSecond()
        return epoch.toString()


    }
    @Throws(NoSuchAlgorithmException::class, InvalidKeyException::class)
    private fun signMessage(timestamp: String, method: String, path: String): String {
        val prehash = timestamp + method + path
        val sha256_HMAC = Mac.getInstance("HmacSHA256")
        val secretDecoded: ByteArray = Base64.getDecoder().decode("MY_API_KEY==")
        val secret_key = SecretKeySpec(secretDecoded, "HmacSHA256")
        sha256_HMAC.init(secret_key)
        return Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(prehash.toByteArray()))
    }
4

1 回答 1

0

正如我所见,在连接消息的组件时,您用于签署消息的代码不使用请求正文 JSON 字符串。这是我的函数版本,它为相同的输入返回与NodeJS 脚本中相同的字符串:

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.util.*
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec

fun main() {
    val result = sign(
        secret = "TVlfQVBJX0tFWQ==",
        timestampMs = System.currentTimeMillis(),
        method = "POST",
        requestPath = "/orders",
        request = Req(
            price = "1.0",
            size = "1.0",
            side = "buy",
            product_id = "BTC-USD",
        )
    )

    println(result)
}

@OptIn(ExperimentalSerializationApi::class)
fun sign(timestampMs: Long, method: String, requestPath: String, request: Req, secret: String): String {
    val accessTimestamp = timestampMs / 1000.0
    val message = "%.3f".format(accessTimestamp) + method + requestPath + Json.encodeToString(request)

    val sha256HMAC = Mac.getInstance("HmacSHA256")
    val key: ByteArray = Base64.getDecoder().decode(secret)
    sha256HMAC.init(SecretKeySpec(key, "HmacSHA256"))

    return Base64.getEncoder().encodeToString(
        sha256HMAC.doFinal(message.toByteArray())
    )
}

@Serializable
data class Req(val price: String, val size: String, val side: String, val product_id: String)
于 2021-10-14T12:18:17.153 回答