BitBay REST API C#

0

Witam Wszystkich!
Jestem w trakcie pisania aplikacji bazującej na REST API udostępnionym przez giełdę BitBay.net
Przy próbie złożenia nowej oferty zakupu na giełdzie otrzymuję zwrotkę:

Result: StatusCode: 415, ReasonPhrase: 'Unsupported Media Type',

Poniżej fragment kodu:
static void Main(string[] args)
{
string timestamp = DateTime.Now.ToString("yyyyMMddHHmmssffff");
string APIKey = "";
string PrivateAPIKey = "";
string APIHash = GenerateHash(PrivateAPIKey, APIKey, timestamp);
var request = SendRequest(APIKey, APIHash, timestamp);
Console.WriteLine("Status: " + request.Status);
Console.WriteLine("Result: " + request.Result);
Console.ReadLine();
}

    static string GenerateHash(string privateKey, string apiKey, string timestamp)
    {
        try
        {
            StringBuilder _stringBuilder = new StringBuilder();
            byte[] privateBytes = Encoding.UTF8.GetBytes(privateKey);
            byte[] bytes = Encoding.UTF8.GetBytes(apiKey + timestamp);
            using (HMACSHA512 _hmac = new HMACSHA512(privateBytes))
            {
                byte[] hash = _hmac.ComputeHash(bytes);
                foreach (byte b in hash)
                {
                    _stringBuilder.Append(b.ToString("X2"));
                }
            }
            return _stringBuilder.ToString();
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }
    static async Task<HttpResponseMessage> SendRequest(string apiKey, string apiHash, string timestamp)
    {
        
        try
        {
            HttpClient client = new HttpClient();

            var values = new Dictionary<string, string>
            {
                {"API-Key", apiKey},
                {"API-Hash",  apiHash},
                {"operation-id", Guid.NewGuid().ToString() },
                {"Request-Timestamp", timestamp},
                {"Content-Type","application/json"},
                {"Body","{amount:0.45,rate:23000,offerType:sell,mode:limit,postOnly:false,fillOrKill:false}"}
            };

            var content = new FormUrlEncodedContent(values);
            return await client.PostAsync("https://api.bitbay.net/rest/trading/offer/BTC-PLN", content);
        }
        catch (Exception ex)
        {
            return null;
        }
    }

Dodam, że podczas prób wywołania metod "GET", które również wymagają autoryzacji wszystko przebiega bez kłopotu.
Z góry dziękuję za pomoc. Pozdrawiam,
Michał.

0

Sprawa jest prosta. Źle wysyłasz request. Oni oczekują, że wyślesz json'a a ty wysyłasz formularz.To twoje "Body" powinno być contentem w requescie. Pozostałe wartości z twojej zmiennej "values" powinny trafić do headerów.

Tak na oko wydaje mi się, że powinieneś wysyłać coś takiego:

HttpClient client = new HttpClient();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://api.bitbay.net/rest/trading/offer/BTC-PLN");

request.Content = new StringContent("{amount:0.45,rate:23000,offerType:sell,mode:limit,postOnly:false,fillOrKill:false}", Encoding.UTF8, "application/json");
request.Headers.Add("API-Key", apiKey);
request.Headers.Add("API-Hash", apiHash);
request.Headers.Add("operation-id", Guid.NewGuid().ToString());
request.Headers.Add("Request-Timestamp", timestamp);

await client.SendAsync(request);
0

Powinno Ci się przydać - oto moja przykładowa implementacja integracji z BitBay (za pomocą RestSharp):

var client = new RestClient(_bitBayPayApiSettings.Value.ApiUrl);
var request = new RestRequest("payments", Method.POST);

var createPaymentBody = new CreatePaymentBody
{
    DestinationCurrency = _bitBayPayApiSettings.Value.DestinationCurrency,
    SourceCurrency = _bitBayPayApiSettings.Value.SourceCurrency,
    Price = price,
    OrderId = orderId,
    NotificationsUrl = _bitBayPayApiSettings.Value.NotificationsUrl
};

var body = SerializeObjectToJsonString(createPaymentBody);
var unixTimestamp = GetUnixTimestamp();
var apiHash = GenerateApiHash(_bitBayPayApiSettings.Value.PublicKey, unixTimestamp, _bitBayPayApiSettings.Value.PrivateKey, body);

request.AddHeader("API-Key", _bitBayPayApiSettings.Value.PublicKey);
request.AddHeader("API-Hash", apiHash);
request.AddHeader("operation-id", Guid.NewGuid().ToString());
request.AddHeader("Request-Timestamp", unixTimestamp.ToString());
request.AddHeader("Content-Type", "application/json");

request.AddJsonBody(body);

var response = await client.ExecuteTaskAsync<CreatePaymentResponse>(request);

return response.Data;

1 użytkowników online, w tym zalogowanych: 0, gości: 1