Not receiving any data from the BitStamp API

0 votes

I have a code but when I execute it, RestSharp BitStamp Authentication fails and I get error:

{"error": "Missing key, signature and nonce parameters"}

The code I am using is as follows:

public ActionResult Index()

        {

            const string BaseUrl = "https://www.bitstamp.net/api/balance/";

            var client = new RestClient();

            var request = new RestRequest();


            client.BaseUrl = BaseUrl;

            AddApiAuthentication(request);

            var response = client.Execute(request);

            var foo = response.Content;

            //{"error": "Missing key, signature and nonce parameters"}

            return View();

        }


        public void AddApiAuthentication(RestRequest restRequest)

        {

            var nonce = DateTime.Now.Ticks;

            var signature = GetSignature(nonce, apiKey, apiSecret, clientId);


            restRequest.AddParameter("key", apiKey);

            restRequest.AddParameter("signature", signature);

            restRequest.AddParameter("nonce", nonce);


        }


        private string GetSignature(long nonce, string key, string secret, string clientId)

        {

            string msg = string.Format("{0}{1}{2}", nonce, clientId, key);

            return ByteArrayToString(SignHMACSHA256(secret, StrinToByteArray(msg))).ToUpper();

        }


        public static byte[] SignHMACSHA256(String key, byte[] data)

        {

            HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));

            return hashMaker.ComputeHash(data);

        }


        public static byte[] StrinToByteArray(string str)

        {

            return System.Text.Encoding.ASCII.GetBytes(str);

        }


        public static string ByteArrayToString(byte[] hash)

        {

            return BitConverter.ToString(hash).Replace("-", "").ToLower();

        }

How to make this work?

Aug 21, 2018 in Blockchain by slayer
• 29,370 points
1,306 views

1 answer to this question.

0 votes

I saw a code online that was used for their project. It is as follows:

public class BitstampAuthenticatedRequest : RestRequest

{

    #region Data

    private readonly string _clientId = "xxxxx"; // Numbers


    private readonly string _apiKey = "xxxxx"; // Random numbers and letters

    private readonly string _apiSecret = "xxxx"; // Random numbers and letters


    private long Nonce = DateTime.Now.Ticks;


    #endregion


    #region Constructor


    public BitstampAuthenticatedRequest(string resource, Method method)

    : base(resource, method)

    {

        this.AddParameter("key", _apiKey);           

        this.AddParameter("nonce", Nonce);

        this.AddParameter("signature", CreateSignature());

    }


    #endregion


    #region Methods


    private string CreateSignature()

    {

        string msg = string.Format("{0}{1}{2}", Nonce,

            _clientId,

            _apiKey);


        return ByteArrayToString(SignHMACSHA256(_apiSecret, StringToByteArray(msg))).ToUpper();

    }


    private static byte[] SignHMACSHA256(String key, byte[] data)

    {

        HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));

        return hashMaker.ComputeHash(data);

    }


    private static byte[] StringToByteArray(string str)

    {

        return System.Text.Encoding.ASCII.GetBytes(str);

    }


    private static string ByteArrayToString(byte[] hash)

    {

        return BitConverter.ToString(hash).Replace("-", "").ToLower();

    }


    #endregion

}


For our new application we used the following code:

public class BitstampAuthenticatedRequest : RestRequest

{

    #region Data

    private readonly string _clientId = "xxxxx"; // Numbers


    private readonly string _apiKey = "xxxxx"; // Random numbers and letters

    private readonly string _apiSecret = "xxxx"; // Random numbers and letters


    private long Nonce = DateTime.Now.Ticks;


    #endregion


    #region Constructor


    public BitstampAuthenticatedRequest(string resource, Method method)

    : base(resource, method)

    {

        this.AddParameter("key", _apiKey);           

        this.AddParameter("nonce", Nonce);

        this.AddParameter("signature", CreateSignature());

    }


    #endregion


    #region Methods


    private string CreateSignature()

    {

        string msg = string.Format("{0}{1}{2}", Nonce,

            _clientId,

            _apiKey);


        return ByteArrayToString(SignHMACSHA256(_apiSecret, StringToByteArray(msg))).ToUpper();

    }


    private static byte[] SignHMACSHA256(String key, byte[] data)

    {

        HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));

        return hashMaker.ComputeHash(data);

    }


    private static byte[] StringToByteArray(string str)

    {

        return System.Text.Encoding.ASCII.GetBytes(str);

    }


    private static string ByteArrayToString(byte[] hash)

    {

        return BitConverter.ToString(hash).Replace("-", "").ToLower();

    }


    #endregion

}

And you have to call it as follows:

        // Decide which url to use

        var baseUrl = "https://www.bitstamp.net/api/balance/";


        // Create the authenticated request

        RestRequest request = new BitstampAuthenticatedRequest(baseUrl, Method.POST);


        // Get the response

        var response = new RestClient().Execute(request);
answered Aug 21, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer

Get specific JSON data value using array from Coin Market Cap API

use inArray function. example: $.getJSON("//api.coinmarketcap.com/v1/ticker/?limit=0", function(data) { ...READ MORE

answered Oct 8, 2018 in Blockchain by Omkar
• 69,220 points
1,837 views
0 votes
1 answer
+2 votes
3 answers

Hyperledger Fabric: Peer Connection error: Failed obtaining connection: Could not connect to any of the endpoints.

 You can extend your /etc/hosts file and make orderer.example.com domain name ...READ MORE

answered Nov 13, 2018 in Blockchain by Omkar
• 69,220 points
3,890 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,428 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Function not working from class

Try this code:  // Add new block ...READ MORE

answered Aug 16, 2018 in Blockchain by digger
• 26,740 points
625 views
0 votes
1 answer

How to parse Json data from remote server?

Yes, you can do it use the ...READ MORE

answered Aug 17, 2018 in Blockchain by digger
• 26,740 points
935 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP