我正在尝试使用F#访问Kraken私有API。访问公共API的代码运行得非常好,但是当我尝试访问私有API时,我总是收到错误" EGeneral:无效的参数"。
#r "FSharp.Data.dll"
open FSharp.Data
open System
open System.Text
open System.Security.Cryptography
let baseUri = "https://api.kraken.com"
let key = MY_KRAKEN_API_KEY
let secret = MY_KRAKEN_API_SECRET
let path = "/0/private/Balance"
let nonce = DateTime.UtcNow.Ticks
let bodyText = "nonce=" + nonce.ToString()
let hmac (key : byte []) (data : byte[]) =
use hmac = new HMACSHA512(key)
hmac.ComputeHash(data)
let sha256 (data : string) =
use sha = SHA256Managed.Create()
sha.ComputeHash(Encoding.UTF8.GetBytes(data))
let createSignature (nonce : int64) body (path : string) secret =
let shaSum = nonce.ToString() + body |> sha256
let data = Array.append (Encoding.UTF8.GetBytes path) shaSum
let key = Convert.FromBase64String secret
hmac key data |> Convert.ToBase64String
let signature = createSignature nonce bodyText path secret
let response = Http.RequestString (
url = baseUri + path,
httpMethod = "POST",
headers = ([("API-Key", key); ("API-Sign", signature)] |> Seq.ofList),
body = TextRequest bodyText
)
有人看到我做错了吗?
编辑: Kraken.com API文档可在此处获取:https://www.kraken.com/help/api
我认为标题签名不正确。该文档要求在标题中提交以下两个值:
API-Key = API密钥API-Sign =使用HMAC-SHA512的消息签名 (URI路径+ SHA256(nonce + POST数据))和base64解码的秘密API 键
编辑2: 其余参数需要使用POST方法传输。就我而言,这只是" nonce" HTTP请求的正文部分中的值。
答案 0 :(得分:4)
我在为Kraken编写C#库时遇到了同样的错误,我发现了这个问题的解决方案:
如果API密钥或标志错误或缺失,则不会出现此错误。问题是您没有为您的请求添加mediatype。我不知道它在F#中是如何工作的,但请看这个例子:
using (var client = new HttpClient())
{
string address = String.Format("{0}/{1}/public/{2}", _url, _version, method);
// Does not work with this:
// var content = new StringContent(postData, Encoding.UTF8);
var content = new StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await client.PostAsync(address, content);
return await response.Content.ReadAsStringAsync();
}
" application / x-www-form-urlencoded" 是关键路径。如果您没有发送请求,则会得到" EGeneral:无效参数" -error。有了它,一切正常。至少在我的情况下。