Coinbase Pro API "invalid signature" RUBY



我正在关注Coinbase pro API和API"无效签名"错误。 我已经附上了我的代码。我也尝试过使用生产帐户,但错误仍然相同。

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帮助将是可观的。 提前致谢:)

我已经对您的代码进行了调试,为什么它不起作用,当我们使用 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

最新更新