public AuthenticationSignature ComputeSignature(IRequest request) { request.TimeStamp = (long)DateTime.UtcNow.ToUnixTimestamp(); string timestamp = DateTime.UtcNow.ToUnixTimestamp().ToString(CultureInfo.InvariantCulture); string prehash = timestamp + request.Method + request.RequestUrl + request.RequestBody; byte[] data = Convert.FromBase64String(Secret); AuthenticationSignature authenticationSignature = new AuthenticationSignature { Signature = HashString(prehash, data), Timestamp = timestamp }; return(authenticationSignature); }
public virtual async Task <string> RequestAsync(IRequest request) { try { await _ioRequestSemaphoreSlim.WaitAsync(); StringContent requestBody = new StringContent(""); HttpResponseMessage response; Uri absoluteUri; if (Authentication == null) { throw new Exception("Invalid Authentication."); } if (string.IsNullOrEmpty(Authentication.Passphrase)) { request.RequestSignature = Authentication.ComputeSignature(request.RequestQuery); request.RequestQuery = null; absoluteUri = request.ComposeRequestUriAbsolute(Authentication.EndpointUrl); HttpClient.DefaultRequestHeaders.Clear(); HttpClient.DefaultRequestHeaders.Add("X-MBX-APIKEY", Authentication.ApiKey); HttpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); //ACCEPT header } else { AuthenticationSignature authenticationSignature = Authentication.ComputeSignature(request); requestBody = request.GetRequestBody(); absoluteUri = request.AbsoluteUri; HttpClient.DefaultRequestHeaders.Clear(); //The api key as a string. HttpClient.DefaultRequestHeaders.Add("CB-ACCESS-KEY", Authentication.ApiKey); //The passphrase you specified when creating the API key. HttpClient.DefaultRequestHeaders.Add("CB-ACCESS-PASSPHRASE", Authentication.Passphrase); //The base64-encoded signature (see Signing a Message). HttpClient.DefaultRequestHeaders.Add("CB-ACCESS-SIGN", authenticationSignature.Signature); // A timestamp for your request. HttpClient.DefaultRequestHeaders.Add("CB-ACCESS-TIMESTAMP", authenticationSignature.Timestamp); //user-agent header HttpClient.DefaultRequestHeaders.Add("User-Agent", "sefbkn.github.io"); } switch (request.Method) { case "GET": response = await HttpClient.GetAsync(absoluteUri); break; case "POST": response = await HttpClient.PostAsync(absoluteUri, requestBody); break; case "DELETE": response = await HttpClient.DeleteAsync(absoluteUri); break; default: throw new NotImplementedException("The supplied HTTP method is not supported: " + request.Method); } if (response == null) { throw new Exception( $"null response from RequestAsync \r\n URI:{request.AbsoluteUri} \r\n:Request Body:{requestBody}"); } return(await response.Content.ReadAsStringAsync()); } catch (Exception ex) { ProcessLogBroadcast?.Invoke(MessageType.Error, $"Method: RequestAsync\r\nException Stack Trace: {ex.StackTrace}"); await ClientWebSocket.ConnectAsync(Authentication.WebSocketUri, CancellationToken.None); } finally { _ioRequestSemaphoreSlim.Release(); } return(null); }