0

希望得到一些帮助,因为这个让我感到困惑......

我在 FTX.com 创建了一个用户帐户和 API 凭据。

他们有一个有趣的身份验证设置,详细信息如下:https ://docs.ftx.com/?python#authentication

他们只提供 python、javascript 和 c# 的代码示例,但我需要在 RoR 应用程序上实现集成。

这是一个链接,它还提供了 GET 和 POST 调用的示例:https ://blog.ftx.com/blog/api-authentication/

我在用着:

ruby '3.0.1'

gem 'rails', '~> 6.1.4', '>= 6.1.4.1'

还,

require 'uri'
require 'net/https'
require 'net/http'
require 'json'

我得到了适用于 GET 调用的身份验证,如下所示:

def get_market
 get_market_url = 'https://ftx.com/api/markets/BTC-PERP/orderbook?depth=20'

 api_get_call(get_market_url)
end
def api_get_call(url)
    ts = (Time.now.to_f * 1000).to_i

    signature_payload = "#{ts}GET/api/markets"

    key = ENV['FTX_API_SECRET']
    data = signature_payload
    digest = OpenSSL::Digest.new('sha256')

    signature = OpenSSL::HMAC.hexdigest(digest, key, data)

    headers = {
      'FTX-KEY': ENV['FTX_API_KEY'],
      'FTX-SIGN': signature,
      'FTX-TS': ts.to_s
    }
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.read_timeout = 1200
    http.use_ssl = true
    rsp = http.get(uri, headers)
    JSON.parse(rsp.body)
  end

这很好用,我得到了正确的回应:

=>
{"success"=>true,
"result"=>
{"bids"=>
[[64326.0, 2.0309],
...
[64303.0, 3.1067]],
"asks"=>
[[64327.0, 4.647],
...
[64352.0, 0.01]]}}

但是,我似乎无法正确验证 POST 调用(尽管据我所知,我正确地遵循了说明)。我使用以下内容:

  def create_subaccount
    create_subaccount_url = 'https://ftx.com/api/subaccounts'

    call_body =
      {
        "nickname": "sub2",
      }.to_json

    api_post_call(create_subaccount_url, call_body)
  end
  def api_post_call(url, body)
    ts = (Time.now.to_f * 1000).to_i

    signature_payload = "#{ts}POST/api/subaccounts#{body}"

    key = ENV['FTX_API_SECRET']
    data = signature_payload
    digest = OpenSSL::Digest.new('sha256')

    signature = OpenSSL::HMAC.hexdigest(digest, key, data)

    headers = {
      'FTX-KEY': ENV['FTX_API_KEY'],
      'FTX-SIGN': signature,
      'FTX-TS': ts.to_s
    }

    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.read_timeout = 1200
    http.use_ssl = true
    request = Net::HTTP::Post.new(uri, headers)
    request.body = body
    response = http.request(request)

    JSON.parse(response.body)
  end

还尝试通过request[]直接传递标头:

  def api_post_call(url, body)
    ts = (Time.now.to_f * 1000).to_i

    signature_payload = "#{ts}POST/api/subaccounts#{body}"

    key = ENV['FTX_API_SECRET']
    data = signature_payload
    digest = OpenSSL::Digest.new('sha256')

    signature = OpenSSL::HMAC.hexdigest(digest, key, data)

    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.read_timeout = 1200
    http.use_ssl = true
    request = Net::HTTP::Post.new(uri)
    request['FTX-KEY'] = ENV['FTX_API_KEY']
    request['FTX-SIGN'] = signature
    request['FTX-TS'] = ts.to_s
    request.body = body
    response = http.request(request)

    JSON.parse(response.body)
  end

这是错误响应: => {"success"=>false, "error"=>"Not logged in: Invalid signature"}

我的感觉是,在通过 HMAC 在这里生成签名之前,将正文添加到 signature_payload 的某个地方..?: signature_payload = "#{ts}POST/api/subaccounts#{body}"

这么想是因为,如果我离开#{body}这里,就像这样: signature_payload = "#{ts}POST/api/subaccounts" 响应是: => {"success"=>false, "error"=>"Missing parameter nickname"}

我已经尝试了使用各种不同的net/https示例设置 POST 调用方法的几次迭代,但没有运气......我也联系了 FTX 支持,但没有任何回应。

如果有人对我在这里做错了什么有一些见解,我会非常感激?

4

0 回答 0