2 years ago

#65486

test-img

Rivian

Algotrading: MEXC Authentication issue C#

I'm building a MEXC connector and I use their official docs here: https://mxcdevelop.github.io/APIDoc/open.api.v2.en.html#request-format

I have an issue with the authentication method they provided. I have an authentication function which I took from their example code in the docs (which is here). I use RestSharp to connect to the REST API. However, I get authentication errors when I try to post a spot trade order. Other methods work fine which still have authentication. Here is my Place Order method:

 public async Task<PlaceOrder> GetPlaceOrderAsync(string symbol, string price, string quantity, string trade_type, string order_type)
        {
            PlaceOrder result = new PlaceOrder();

            var Params = new Dictionary<string, string>
            {
                {"order_type", order_type },
                {"price" , price },
                {"quantity", quantity },
                {"symbol" , symbol },
                {"trade_type", trade_type },
            };

            var serializedParams = JsonConvert.SerializeObject(Params);

            var paramString = getRequestParamString(Params);
            var encodedString = urlEncode(paramString);

            var Timestamp = DateTime.UtcNow.ToUnixTimeMilliseconds().ToString();
            var signature = sign(Timestamp, paramString);
            var cleanedSignature = signature.Replace("-", "");

            var restClient = new RestClient();
            var restRequest = new RestRequest("https://www.mexc.com/open/api/v2/order/place?" + 
                "api_key=" + this.ApiKey + 
                "&req_time=" + Timestamp + 
                "&sign=" + cleanedSignature,
                Method.POST);

            restRequest.AddHeader("ApiKey", this.ApiKey);
            restRequest.AddHeader("Signature", cleanedSignature.ToLowerInvariant());
            restRequest.AddHeader("Request-Time", Timestamp);
            restRequest.AddHeader("content-type", "application/json");

            restRequest.AddBody(serializedParams);

            restRequest.RequestFormat = DataFormat.Json;
            var restResponse = await restClient.ExecuteAsync(restRequest);
            result = JsonConvert.DeserializeObject<PlaceOrder>(restResponse.Content);

            return result;

What I get in return as a response is this:

"{"msg":"authorize failed","code":401}"

Which indicates that there is something wrong with the authentication but I have used the same authentication method in other private methods which require authentication. For comparison here is a User Balance checking method, note that balance method does not require any parameters so I'm sending an empty dictionary:

public  async Task<Balance> GetBalanceAsync()
        {
            var Params = new Dictionary<string, string>
            {

            };

            var paramString = getRequestParamString(Params);
            var encodedString = urlEncode(paramString);

            var Timestamp = DateTime.UtcNow.ToUnixTimeMilliseconds().ToString();
            
            var signature = sign(Timestamp, encodedString);
            var cleanedSignature = signature.Replace("-", "");

            Balance result = new Balance();

            var restClient = new RestClient();
            var restRequest = new RestRequest("https://www.mexc.com/open/api/v2/account/info", Method.GET);

            restRequest.AddHeader("ApiKey", this.ApiKey);
            restRequest.AddHeader("Signature", cleanedSignature.ToLowerInvariant());
            restRequest.AddHeader("Request-Time", Timestamp);

            var restResponse = await restClient.ExecuteAsync(restRequest);

            result = JsonConvert.DeserializeObject<Balance>(restResponse.Content);

            return result;
        }

If anyone could point out something that I can't see I would really appreciate it. MEXC integration is not very widespread on the net so I'm struggling to find other examples.

c#

rest

trading

algorithmic-trading

cryptocurrency

0 Answers

Your Answer

Accepted video resources