ruby - 使用 Ruby 连接到 Coinbase API 并遇到连接问题。法拉第
问题描述
尝试使用 Ruby 连接到 Coinbase GDAX 沙盒 API。目前使用 Faraday,但欢迎 HTTParty 或 Ruby Core API 建议。
我很难发出帖子请求,似乎无法说出我在哪里搞砸了。
出现了一份错误清单,但最重要的是:
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/header.rb:18:in
block in initialize_http_header': undefined method
strip' for 1556021965:Fixnum (NoMethodError)
Coinbase 文档:https ://docs.pro.coinbase.com/?ruby#signing-a-message
简而言之,我的 api 调用。
payload = {
"data": "DATA"
}
url = 'https://api-public.sandbox.gdax.com/orders'
conn = Faraday.new
response = conn.post do |req|
req.url url
req.headers['REQ-HEADER-1'] = "FOO",
req.headers['REQ-HEADER-1'] = "BAR",
req.body = payload
end
puts response
完整代码
require 'Faraday' require 'base64' require 'openssl' require 'json'
class foo
def initialize
@api_key = '25b1d259417d159443c03ed14eb9ee49'
@api_secret = 'trBTZUj4zLfHEBfbQy3+LeLOaAeAbfG/zCqIvzu3RyiKhqf5y85NYqqZi7GUSBCzhryud1pyOs5idibzdOx/tw=='
@api_pass = '7zkf0bvr6q4'
end
def signature(request_path='', body='', timestamp=nil, method='POST')
body = body.to_json if body.is_a?(Hash)
timestamp = Time.now.to_i if !timestamp
what = "#{timestamp}#{method}#{request_path}#{body}";
# create a sha256 hmac with the secret
secret = Base64.decode64(@api_secret)
hash = OpenSSL::HMAC.digest('sha256', secret, what)
Base64.strict_encode64(hash)
end
def post_buy
usd = get_usd_available
usd = usd / 2
payload = {
"funds": usd.to_s,
"product_id": "BTC-USD",
"side": "buy",
"type": "market"
}
url = 'https://api-public.sandbox.gdax.com/orders'
conn = Faraday.new
response = conn.post do |req|
req.url url
req.headers['CB-ACCESS-KEY'] = @api_key,
req.headers['CB-ACCESS-SIGN'] = signature( url, payload),
req.headers['CB-ACCESS-TIMESTAMP'] = Time.now.to_i,
req.headers['CB-ACCESS-PASSPHRASE'] = @passphrase,
req.body = payload
end
puts response
end
end
foo.new.post_buy
解决方案
您可以在http/header.rb检查代码:它调用strip
每个标头,因此每个标头值都应该是一个字符串。
我认为这是有问题的行,您在这里提供了一个整数作为标题值:
req.headers['CB-ACCESS-TIMESTAMP'] = Time.now.to_i
将其转换为字符串应该可以修复它:
req.headers['CB-ACCESS-TIMESTAMP'] = Time.now.to_i.to_s
推荐阅读
- java - 我无法从组合框中获得选择
- c# - Xamarin Forms C# ListView 搜索
- angular - 角度集成测试:如何在 MouseEvent 上指定目标
- android - 使用 Xamarin Android RecyclerView 时如何确定“活动”视图
- reactjs - 使用 Redux Saga 从 API 获取信息
- makefile - ifeq 条件和 Makefile
- firebase - 错误:_elements.contains(element) 不正确
- javascript - 如何避免返回到带有 HTML id 属性标记的上一个部分,而是转到上一个网站?
- python-3.x - 如何在 python 3 中对大量文本进行分类?
- php - PHP:允许的内存大小......字节用尽