Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Ruby连接到Coinbase API并遇到连接问题。法拉第_Ruby_Api_Coinbase Api_Faraday - Fatal编程技术网

使用Ruby连接到Coinbase API并遇到连接问题。法拉第

使用Ruby连接到Coinbase API并遇到连接问题。法拉第,ruby,api,coinbase-api,faraday,Ruby,Api,Coinbase Api,Faraday,正在尝试使用Ruby连接到Coinbase GDAX沙盒API。 目前正在使用法拉第,但欢迎HTTParty或rubycoreapi建议 我很难提出发帖请求,似乎不知道我把事情搞砸了 出现错误的详细列表,但最上面的是: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/Ruby/2.3.0/net/http/header.rb:18:in 初始化\u http\u头中的块:未定义的方法 1556021965:Fixnum

正在尝试使用Ruby连接到Coinbase GDAX沙盒API。 目前正在使用法拉第,但欢迎HTTParty或rubycoreapi建议

我很难提出发帖请求,似乎不知道我把事情搞砸了

出现错误的详细列表,但最上面的是:

/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/Ruby/2.3.0/net/http/header.rb:18:in 初始化\u http\u头中的
块:未定义的方法
1556021965:Fixnum(命名错误)

Coinbase文档:

简而言之,我的api调用

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

完整代码

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
您可以在以下位置检查代码:它在每个标题上调用
strip
,因此每个标题值都应该是字符串

我认为这是有问题的一行,您在这里提供了一个整数作为标题值:

req.headers['CB-ACCESS-TIMESTAMP'] = Time.now.to_i
将其转换为字符串应该可以修复它:

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

您是否尝试过将时间戳转换为字符串?并发出请求当我尝试将其转换为字符串时,我得到此响应
{“message”=>“User-Agent头是必需的。”}
&我使用了HTTPparty