mscheetz / KuCoinApi.Net

KuCoin api access built in .Net Standard
MIT License
6 stars 4 forks source link

{"code":"400005","msg":"Invalid KC-API-SIGN"} #10

Closed ahvahsky2008 closed 5 years ago

ahvahsky2008 commented 5 years ago

Hi. I have troubles with lib. Pls help) I think i made a stupid mistake. Whats wrong here?

public static string apiKey = "xxx";
public static string apiSecret = "yyy";
public static string apiPassword = "zzz";
static void Main(string[] args) {

 var clientOid = Guid.NewGuid().ToString().Replace("-", "");
 var body = new SortedDictionary <string,  object>();
 body.Add("clientOid", clientOid);
 body.Add("symbol", "BTC-LTC");
 body.Add("price", 0.1);
 body.Add("side", "BUY");
 body.Add("size", 0.6);
 body.Add("type", "LIMIT");

 var timestamp0 = Convert.ToInt64(Regex.Match(new WebClient().DownloadString("https://openapi-v2.kucoin.com/api/v1/timestamp"), "(?<=data\":).+(?=})").Value);

 var headers = GetRequestHeaders(HttpMethod.Post, "https://openapi-v2.kucoin.com/api/v1/orders",
  Convert.ToInt64(timestamp0), body);

 var request = (HttpWebRequest) WebRequest.Create("https://openapi-v2.kucoin.com/api/v1/orders");
 request.ContentType = "application/json";
 request.Method = "POST";

 foreach(KeyValuePair <string, string> entry in headers) {
  request.Headers.Add(entry.Key, entry.Value);
 }
 using(var streamWriter = new StreamWriter(request.GetRequestStream())) {
  string json = JsonConvert.SerializeObject(headers);
  streamWriter.Write(json);
 }

 var response = (HttpWebResponse) request.GetResponse();
 using(var streamReader = new StreamReader(response.GetResponseStream())) {
  var result = streamReader.ReadToEnd();
 }
}

private static Dictionary <string, string> GetRequestHeaders(HttpMethod httpMethod, string endpoint, long timestamp, SortedDictionary <string, object> body = null) {
 var headers = new Dictionary <string, string>();

 headers.Add("KC-API-KEY", apiKey);
 headers.Add("KC-API-SIGN", new Security().GetSignature(httpMethod.ToString(), endpoint, timestamp, apiSecret, body));
 headers.Add("KC-API-TIMESTAMP", timestamp.ToString());
 headers.Add("KC-API-PASSPHRASE", apiPassword);

 return headers;
}
mscheetz commented 5 years ago

You needed the endpoint when signing see here: https://github.com/mscheetz/KuCoinApi.Net/blob/v2.0/KuCoinApi.Net/KuCoinApi.Net/Core/Security.cs

In your case you'd pass '/api/v1/orders'

On Sun, Mar 3, 2019, 3:07 AM ahvahsky2008 notifications@github.com wrote:

Hi. I have troubles with lib. Pls help) I think i made a stupid mistake. Whats wrong here?

public static string apiKey = "xxx"; public static string apiSecret = "yyy"; public static string apiPassword = "zzz"; static void Main(string[] args) {

        ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

        var clientOid = Guid.NewGuid().ToString().Replace("-", "");
        var body = new SortedDictionary<string, object>();
        body.Add("clientOid", clientOid);
        body.Add("symbol", "BTC-LTC");
        body.Add("price",0.1 );
        body.Add("side", "BUY");
        body.Add("size", 0.6);
        body.Add("type", "LIMIT");

        var timestamp0= Convert.ToInt64(Regex.Match(new WebClient().
            DownloadString("https://openapi-v2.kucoin.com/api/v1/timestamp"), "(?<=data\":).+(?=})").Value);

        var headers = GetRequestHeaders(HttpMethod.Post, "https://openapi-v2.kucoin.com/api/v1/orders",
            Convert.ToInt64(timestamp0), body);

        var request = (HttpWebRequest)WebRequest.Create("https://openapi-v2.kucoin.com/api/v1/orders");
        request.ContentType = "application/json";
        request.Method = "POST";

        foreach (KeyValuePair<string, string> entry in headers)
        {
            request.Headers.Add(entry.Key, entry.Value);
        }

        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = JsonConvert.SerializeObject(headers);

            streamWriter.Write(json);
        }

        var response = (HttpWebResponse)request.GetResponse();
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }
    }
    private static Dictionary<string, string> GetRequestHeaders(HttpMethod httpMethod, string endpoint, long timestamp, SortedDictionary<string, object> body = null)
    {
        var headers = new Dictionary<string, string>();

        headers.Add("KC-API-KEY", apiKey);
        headers.Add("KC-API-SIGN", new Security().GetSignature(httpMethod.ToString(), endpoint, timestamp, apiSecret, body));
        headers.Add("KC-API-TIMESTAMP", timestamp.ToString());
        headers.Add("KC-API-PASSPHRASE", apiPassword);

        return headers;
    }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mscheetz/KuCoinApi.Net/issues/10, or mute the thread https://github.com/notifications/unsubscribe-auth/AKs-IrOugX07yKMfyQhvR5Jm2Cwg1GWLks5vS4LZgaJpZM4ba2oG .