【发布时间】: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