我目前正在尝试连接到CEX.IO比特币交易所的websocket。 Websocket连接没问题,但在验证时,我有错误:
Timestamp is not in 20sec range。 我不知道这个错误是什么。createSignature的测试用例1和2正常( https://cex.io/websocket-api#authentication )。
认证Go代码:
I'm currently trying to connect to the CEX.IO bitcoin exchange's websocket. Websocket connection is OK but at the time of authentication, I have the error:
Timestamp is not in 20sec range. I don't know what this error.Test case 1 & 2 for createSignature is OK (https://cex.io/websocket-api#authentication).
Authentication Go code :
func toHmac256(message string, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
}
func Signature() (string, string) {
nonce := time.Now().Unix() // Edit with Cerise Limón answer
message := strconv.FormatInt(nonce, 10) + "API-KEY"
signature := api.toHmac256(message, "SECRET-KEY")
return signature, nonce
}
func toHmac256(message string, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
}
func Signature() (string, string) {
nonce := time.Now().Unix() // Edit with Cerise Limón answer
message := strconv.FormatInt(nonce, 10) + "API-KEY"
signature := api.toHmac256(message, "SECRET-KEY")
return signature, nonce
}
错误消息告诉您时间戳距离当前时间超过20秒。
API预计时间以秒为单位,而不是纳秒。 使用Unix以秒为单位获取时间。
Unix时间是自1970年1月1日UTC以来的秒数。
如果此操作失败,请检查您的系统时间是否正确设置为秒。
The error message is telling you that the timestamp is more than 20 seconds away from the current time.
The API expects time in seconds, not nanoseconds. Use Unix to get the time in seconds.
Unix time is is seconds since Jan 01 1970 UTC.
If this fails, then check that your system time is set correctly down to the second.
nonce := time.Now().Unix()
nonce := time.Now().Unix()