【发布时间】:2019-01-31 10:34:05
【问题描述】:
我尝试向新的 okex api 发送经过身份验证的请求。 他们的一部分documentation:
创建请求
所有 REST 请求必须包含以下标头:
OK-ACCESS-KEY api键作为字符串。
OK-ACCESS-SIGN base64 编码的签名(请参阅签署消息)。
OK-ACCESS-TIMESTAMP 请求的时间戳。
OK-ACCESS-PASSPHRASE 创建 API 密钥时指定的密码。
所有请求正文都应具有内容类型 application/json 并且是有效的 JSON。
签署消息
OK-ACCESS-SIGN 标头是通过在 prehash 字符串时间戳 + 方法 + requestPath + 正文(其中 + 表示字符串连接)上使用 base64 解码的密钥创建 sha256 HMAC 生成的,secretKey 和 base64 编码输出.例如:sign=CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(timestamp + 'GET' + '/users/self/verify', secretKey))
时间戳值与 OK-ACCESS-TIMESTAMP 标头和纳米精度相同。
方法应为大写,如 GET/POST。
requestPath 为请求端点的路径,如:/orders?before=2&limit=30。
body 是请求正文字符串,如果没有请求正文,则省略(通常用于 GET 请求)。例如:{"product_id":"BTC-USD-0309","order_id":"377454671037440"}
secretKey 在用户订阅 Apikey 时生成。 Prehash 字符串:2018-03-08T10:59:25.789ZPOST/orders?before=2&limit=30{"product_id":"BTC-USD-0309","order_id":"377454671037440"}
这就是我尝试的方法
BASE_URL = 'https://www.okex.com'.freeze
def base_api_endpoint
url = URI.parse BASE_URL
"#{url.scheme}://#{url.host}:#{url.port}"
end
def authenticated_request(method, url, options = {}, paginated: false)
raise Exceptions::InvalidApiKey if @key.blank? || @secret.blank? || @passphrase.blank?
response = rest_connection.run_request(method, URI.encode(url), nil, nil) do |req|
if method == :post
req.body = options.to_json
else
req.params.merge! options
end
end
def rest_connection
@conn ||= new_rest_connection
end
TimestampRequestMiddleware = Struct.new(:app) do
def call(env)
env[:request_headers].merge!('OK-ACCESS-TIMESTAMP' => Time.now.utc.iso8601(3))
app.call env
end
end
SignRequestMiddleware = Struct.new(:app, :key, :secret, :passphrase) do
def call(env)
request_path = env[:url].path
if env[:url].query.present?
request_path += ('?' + env[:url].query)
end
timestamp = env[:request_headers]['OK-ACCESS-TIMESTAMP']
method = env[:method].to_s.upcase
what = "#{timestamp} + #{method} + #{request_path} + #{env.body}"
decoded_secret = Base64.decode64(secret)
mac = OpenSSL::HMAC.digest('sha256', decoded_secret, what)
sign = Base64.strict_encode64(mac)
env[:request_headers].merge!('OK-ACCESS-KEY' => key, 'OK-ACCESS-SIGN' => sign, 'OK-ACCESS-TIMESTAMP' => timestamp, 'OK-ACCESS-PASSPHRASE' => passphrase)
app.call env
end
end
def new_rest_connection
Faraday.new( base_api_endpoint, { ssl: { verify: false } }) do |conn|
conn.use Market::OkexMarket::OkexCustomErrors, api_key: @key
conn.request :json
conn.response :json, content_type: /\bjson$/
conn.response :logger, Logger.new(STDOUT) , bodies: true if Rails.env.development?
conn.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
conn.use TimestampRequestMiddleware
conn.use SignRequestMiddleware, @key, @secret, @passphrase
conn.headers['Content-Type'] = 'application/json'
conn.adapter :net_http
end
end
def complete_balances
data = authenticated_request(:get, '/api/spot/v3/accounts')
data.map do |d|
[
d['currency'],
{'available' => d['available'].to_f, 'onOrders' => d['hold'].to_f}
]
end.to_h
end
但是当我调用 complete_balances 方法时,我从 okex 收到了一个错误 响应:{"code"=>30013, "message"=>"Invalid Sign"} 我不知道我错在哪里。有人可以帮我吗?
【问题讨论】:
标签: ruby-on-rails hmac faraday