【问题标题】:Coinbase Pro API "invalid signature" RUBYCoinbase Pro API “无效签名” RUBY
【发布时间】:2020-04-09 23:12:40
【问题描述】:

我正在关注 Coinbase pro API 和 API 'invalid signature' 错误。我附上了我的代码。我也尝试使用生产帐户,但错误仍然相同。

  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

控制台> Coinbase.new.hit_coinbase_api 帮助将是可观的。在此先感谢:)

【问题讨论】:

  • 请附上您的代码。
  • 我已经更新了代码。谢谢:)

标签: ruby authentication coinbase-api


【解决方案1】:

我已经对您的代码进行了调试,为什么它不起作用,看起来当我们使用 Net::HTTP 时存在标头问题。我使用 http wrapper 来调用 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 的原因,请告诉我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 2019-01-12
    • 2015-04-21
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    相关资源
    最近更新 更多