【问题标题】:Lua vs Python for bitstamp API. Python code works but not Lua. Why?用于 bitstamp API 的 Lua 与 Python。 Python 代码有效,但 Lua 无效。为什么?
【发布时间】:2019-12-21 09:26:25
【问题描述】:

我的爱好是通过 API 买卖比特币,我已经成功地使用 python 通过使用 lua 也存在的“请求”库来做到这一点。我正在使用 bitstamp API。但是,我的 lua 代码不起作用。

这是适用于 python 的代码

import time
import hashlib
import hmac
import requests
import ast
import pprint

nonce = str(int(time.time()))
customer_id = 'some id'
api_key = 'some string'
API_SECRET = 'some other string'

message = nonce + customer_id + api_key

signature = hmac.new(API_SECRET, msg=message, digestmod=hashlib.sha256).hexdigest().upper()

    # --------------Requests Parameters------------------------

params = {"key": api_key, "signature": signature, 'nonce': nonce}

    # --------------Get Account Balance--------------------------

r = requests.post('https://www.bitstamp.net/api/v2/balance/', data=params)

print(r.text)

现在这段代码有效,我得到了成功的响应。

但是我的 lua 代码不起作用,因为我从 API 收到错误,所以这显然不是语法错误。这是lua代码。

requests= require("requests")
openssl = require("openssl")

nonce = tostring(os.time())
customer_id = 'some id'
api_key = 'some string'
API_SECRET = 'some other string'

message = nonce..customer_id..api_key
signature = openssl.hmac.digest('sha256',message,API_SECRET)
signature = string.upper(signature)

print(signature)

params = {['key'] = api_key, ['signature'] = signature, ['nonce'] = nonce}

r = requests.post{"https://www.bitstamp.net/api/v2/balance/", data = params}
print(r.text)
print(r.status_code)

我从此代码从服务器得到的响应是:

{"status": "error", "reason": "Missing key, signature and nonce parameters", "code": "API0000"} 403

值 API0000 是来自 api(https://www.bitstamp.net/api/) 的错误代码,表示“参数未在 API 请求中发布”

我只是不知道我在这里做错了什么。

【问题讨论】:

  • 您可以将数据发送到httpbin.org/post,它会发回您请求中的所有标头、cookie、数据,以便您可以比较python和lua的结果
  • 感谢弗拉斯。会调查的。

标签: python lua


【解决方案1】:

使用 http://httpbin.org/post 我看到 Lua 将其发送为 "data" 但 Python 将其发送为 "form" 并带有标头 "Content-Type:application/x-www-form-urlencoded"

我使用

得到相同的结果
params = "key=some string&nonce=1565817138&signature=7DE82CB42D1B8F38F7AC1A74EC3A5E06F1AA3A20A14F3DD0E7040F088FBF9F69"

headers = {["Content-Type"] = "application/x-www-form-urlencoded"}

url = "http://httpbin.org/post"
-- url = "https://www.bitstamp.net/api/v2/balance/"

r = requests.post{url, data = params, headers = headers}

顺便说一句:params 不能是字典/表格,因为它会将其转换为

"{\"key\":\"some string\",\"nonce\":\"1565888007\", ...

而且我不太了解 Lua,无法将字典转换为预期的字符串。


编辑:我创建这个是为了将params 转换为正确的字符串

params = {['key'] = api_key, ['signature'] = signature, ['nonce'] = nonce}

params_str = {}
for k, v in pairs(params) do 
    table.insert(params_str, k..'='..v)
end
params = table.concat(params_str, '&')

print('params> '..params)

结果:

params> signature=1492CA16012C0A6F9AAF7DDD66F649FF1DCF0E675D743AEBF77E0E437C8FF006&key=some string&nonce=1565889236

完整代码:

requests= require("requests")
openssl = require("openssl")

nonce = tostring(os.time())
customer_id = 'some id'
api_key = 'some string'
API_SECRET = 'some other string'

message = nonce..customer_id..api_key
signature = openssl.hmac.digest('sha256', message, API_SECRET)
signature = string.upper(signature)

params = {['key'] = api_key, ['signature'] = signature, ['nonce'] = nonce}

params_str = {}
for k, v in pairs(params) do 
    table.insert(params_str, k..'='..v)
end
params = table.concat(params_str, '&')

-- params = "key=some string&nonce=1565817138&signature=7DE82CB42D1B8F38F7AC1A74EC3A5E06F1AA3A20A14F3DD0E7040F088FBF9F69"
print('params> '..params)

url = "http://httpbin.org/post"
-- url = "https://www.bitstamp.net/api/v2/balance/"

headers = {["Content-Type"] = "application/x-www-form-urlencoded"}

r = requests.post{url, data = params, headers = headers}
print(r.text)
print(r.status_code)

【讨论】:

  • 你明白了。这解决了我的问题。这个 Lua 代码现在与 bitstamp 完美配合。感谢您的努力,因为我现在对 lua 有了更多的了解。
猜你喜欢
  • 2014-08-08
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 2017-01-14
  • 1970-01-01
  • 2020-01-14
  • 2012-06-14
  • 2019-09-10
相关资源
最近更新 更多