我尝试向新的okex api发送经过身份验证的请求。他们documentation的一部分
创建请求
所有REST请求必须包含以下标头:
OK-ACCESS-KEY将api密钥作为字符串。
OK-ACCESS-对base64编码的签名进行签名(请参见对消息进行签名)。
OK-ACCESS-TIMESTAMP请求的时间戳。
OK-ACCESS-PASSPHRASE您在创建API密钥时指定的密码。
所有请求主体都应该具有内容类型application/json,并且是有效的JSON。
对消息进行签名
OK-ACCESS-SIGN报头是通过使用sha256解码的密钥创建一个Base64解码的HMAC来生成的,该密钥使用的是绝对字符串timestamp + method + requestPath + body (其中+表示字符串连接)、secretKey和base64-encode输出。例如: sign=CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(timestamp + 'GET‘+ '/users/self/verify',secretKey))
时间戳值与OK-ACCESS-TIMESTAMP头部相同,精度为纳米级。
方法应该是大写的,如GET/POST。
限制为请求端点的路径,如:/orders? requestPath =2&limit=30。
body是请求正文字符串,如果没有请求正文,则忽略该字符串(通常用于GET请求)。例如:{“product_id”:“BTC-USD-0309”,"order_id":"377454671037440"}
secretKey是在用户订阅Apikey时生成的。预哈希string: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响应中得到了一个错误:{“=>30013”code,"message"=>"Invalid Sign"},并且我无法识别我错在哪里。有人能帮我一下吗?
发布于 2019-03-13 21:18:50
"...使用base64解码的密钥...“我也面临着同样的问题。在检查了他们的示例之后,我发现密钥不需要是base64解码的。也许我们应该假设我们已经得到了一个base64解码的密钥。无论如何,一旦你停止base64解码密钥,你的签名生成器就应该开始工作了。我的是这样的。
https://stackoverflow.com/questions/54458572
复制相似问题