Search code examples
rubyapicoinbase-apifaraday

Connecting to Coinbase API with Ruby and running into connection issue. Faraday


Attempting to connect to the Coinbase GDAX sandbox API with Ruby. Currently using Faraday, but HTTParty or Ruby Core API advice is welcomed.

I am having a hard time making post requests and can't seem to tell where I am messing this up.

A laundry list of error's appear but the top one is:

/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/header.rb:18:in block in initialize_http_header': undefined methodstrip' for 1556021965:Fixnum (NoMethodError)

Coinbase docs: https://docs.pro.coinbase.com/?ruby#signing-a-message

My api call in a nutshell.

payload = { 
            "data": "DATA"        
        }

        url = 'https://api-public.sandbox.gdax.com/orders'
        conn = Faraday.new      
        response = conn.post do |req|           
            req.url url
            req.headers['REQ-HEADER-1'] = "FOO",
            req.headers['REQ-HEADER-1'] = "BAR",           
            req.body = payload      
        end

        puts response

Full Code

require 'Faraday' require 'base64' require 'openssl' require 'json'


class foo


    def initialize
        @api_key = '25b1d259417d159443c03ed14eb9ee49'       
        @api_secret = 'trBTZUj4zLfHEBfbQy3+LeLOaAeAbfG/zCqIvzu3RyiKhqf5y85NYqqZi7GUSBCzhryud1pyOs5idibzdOx/tw=='        
        @api_pass = '7zkf0bvr6q4'       
    end         

    def signature(request_path='', body='', timestamp=nil, method='POST')
      body = body.to_json if body.is_a?(Hash)
      timestamp = Time.now.to_i if !timestamp

      what = "#{timestamp}#{method}#{request_path}#{body}";

      # create a sha256 hmac with the secret
      secret = Base64.decode64(@api_secret)
      hash  = OpenSSL::HMAC.digest('sha256', secret, what)
      Base64.strict_encode64(hash)
    end

    def post_buy

        usd = get_usd_available         
        usd = usd / 2       

        payload = { 
            "funds": usd.to_s,
            "product_id": "BTC-USD",
            "side": "buy",
            "type": "market"        
        }

        url = 'https://api-public.sandbox.gdax.com/orders'
        conn = Faraday.new      
        response = conn.post do |req|           
            req.url url
            req.headers['CB-ACCESS-KEY'] = @api_key,
            req.headers['CB-ACCESS-SIGN'] = signature( url, payload),
            req.headers['CB-ACCESS-TIMESTAMP'] = Time.now.to_i,
            req.headers['CB-ACCESS-PASSPHRASE'] = @passphrase,           
            req.body = payload      
        end

        puts response

    end

end

foo.new.post_buy

Solution

  • You can check the code at http/header.rb: it calls strip on every header, so every header value should be a string.

    I think this is the offending line, you are supplying an integer here as the header value:

    req.headers['CB-ACCESS-TIMESTAMP'] = Time.now.to_i
    

    Converting it to string should fix it:

    req.headers['CB-ACCESS-TIMESTAMP'] = Time.now.to_i.to_s