Coinbase Pro API“无效签名” RUBY

时间:2019-12-17 12:37:50

标签: ruby authentication coinbase-api

我正在跟踪Coinbase pro API和API“无效签名”错误。我已附上我的代码。我也尝试使用生产帐户,但错误仍然相同。

while true ; do status=`sapcontrol -nr ${INST_NUM} -function GetProcessList |awk -F ',' '{if ($3) print $3;}'|grep -i GREEN | wc -l`; if [[ $status -ge 2 ]]; then break ;fi; done

控制台> class Coinbase def initialize() @end_point = 'https://api-public.sandbox.pro.coinbase.com' @key = "b34ae4ffd38acfc3f11e272654fa77c4" @secret = "0k+YreiCq5tY3UdShw0VB0RI/kKiLv1vNGpNKpaDzDLtVPFNzlMGgoFljYRO4qsH5KCZ9M5upnq5/rxSVzENdg==" @passphrase = "50fyu9n04nu" @timestamp = Time.now.to_i @sign = access_signature end def access_signature request_path="/accounts" method='GET' body='' body = body.to_json if body.is_a?(Hash) timestamp = @timestamp what = "#{timestamp}#{method}#{request_path}#{body}"; secret = Base64.decode64(@secret) hash = OpenSSL::HMAC.digest('sha256', secret, what) Base64.strict_encode64(hash) end def hit_coinbase_api call :get, "/accounts" end def call(method, path, params = {}, parse_response_as_json = true, with_auth_token = true) uri = URI(@end_point + path) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = nil if method == :get request = Net::HTTP::Get.new(uri.request_uri) else raise 'Unsupported request method' end request.body = params.to_json request.add_field('Content-Type', 'application/json') request.add_field('CB-ACCESS-KEY', @key) request.add_field('CB-ACCESS-SIGN', @sign) request.add_field('CB-ACCESS-TIMESTAMP', @timestamp) request.add_field('CB-ACCESS-PASSPHRASE', @passphrase) response = http.request(request) json_resp = JSON.parse(response.body) puts json_resp end end 帮助将是可观的。在此先感谢:)

1 个答案:

答案 0 :(得分:0)

我已经在您的代码上调试了为什么它不起作用,当我们使用Net :: HTTP时,似乎出现了标题问题。我已经使用http包装器来调用API,并且有效

module Coinbase
  class ApiError < RuntimeError; end
  class Api

    def initialize()
      @end_point = 'https://api-public.sandbox.pro.coinbase.com'
      @key = "b34ae4ffd38acfc3f11e272654fa77c4"
      @secret = "0k+YreiCq5tY3UdShw0VB0RI/kKiLv1vNGpNKpaDzDLtVPFNzlMGgoFljYRO4qsH5KCZ9M5upnq5/rxSVzENdg=="
      @passphrase = "50fyu9n04nu"
      @timestamp = Time.now.to_i
    end

    def access_signature method, request_path, body
      body = body.to_json if body.is_a?(Hash)
      what = "#{@timestamp}#{method}#{request_path}#{body}";
      # create a sha256 hmac with the secret
      secret = Base64.decode64(@secret)
      hash  = OpenSSL::HMAC.digest('sha256', secret, what)
      Base64.strict_encode64(hash)
    end

    def hit_coinbase_api
       call "GET",  "/accounts"
    end

    private

    def call(method, request_path, params = nil)
      @sign = access_signature(method, request_path, params)
      @headers = {
        "Content-Type" => "application/json",
        "Accept" => "application/json",
        "CB-ACCESS-KEY" => @key,
        "CB-ACCESS-SIGN" => @sign,
        "CB-ACCESS-TIMESTAMP" => @timestamp,
        "CB-ACCESS-PASSPHRASE" => @passphrase,
        "User-Agent"=> 'request'
      }
      if method=='GET'
        response = HTTParty.get("#{@end_point}#{request_path}",
                { 
                  :headers => @headers
                })
      else 
        response = HTTParty.post("#{@end_point}#{request_path}",
                { 
                  :body => params.to_json,
                  :headers => @headers
                })
      end
      JSON.parse(response.body)
    end
  end
end

让我知道是否有人找到了为什么它不能与Net :: HTTP一起使用