- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 MtGox.com WebSocket API 进行身份验证,并在很长一段时间后设法完成了 JSON 数据所需的“调用”属性。然而,我意识到我正在使用 Python 2 来运行我的代码示例,而 API 最终将在其中实现的应用程序是用 Python 3 编写的。当我试图让它在 Python 3 中工作时,我遇到了几个问题我尽管进行了多次长时间的尝试,仍无法解决。
我也尝试过 2to3
,但它似乎没有针对此类问题的内置修复程序。
可在此处找到经过身份验证的 API 调用的 API 规范: https://en.bitcoin.it/wiki/MtGox/API/Streaming#Authenticated_commands
这是我用于生成 JSON 调用的工作 Python 2 脚本,然后我通过我为 Chrome 找到的 WebSocket 控制台扩展运行它。
import hashlib
import time
import hmac
import json
import base64
import binascii
apikey = ""
apisecret = ""
def _nonce():
"""produce a unique nonce that is guaranteed to be ever increasing"""
microtime = int(time.time() * 1E6)
return microtime
def _reqid(nonce):
return hashlib.md5(str(nonce)).hexdigest()
def send_signed_call(api_endpoint, params):
nonce = _nonce()
reqid = _reqid(nonce)
call = json.dumps({
"id" : reqid,
"nonce" : nonce,
"call" : api_endpoint,
"params" : params,
})
sign = hmac.new(base64.b64decode(apisecret), call, hashlib.sha512).digest()
signedcall = apikey.replace("-", "").decode("hex") + sign + call
return json.dumps({
"op" : "call",
"call" : base64.b64encode(signedcall),
"id" : reqid,
"context" : "mtgox.com"
})
msg = send_signed_call("private/info", {})
print(msg)
我遇到的一些错误与不再存在的 String.decode("hex") 有关,我还有其他一些错误,但不幸的是我没有跟踪所有错误,因为我尝试了很多不同的方法。我还查看了其他语言的相同功能的代码示例,但找不到与 Python 3 问题相关的任何线索。很多事情似乎与 Python 3 中对字节和字符串编码和解码所做的更改有关。
提前致谢!
最佳答案
终于解决了!
这是 send_signed_call
函数的工作版本,享受:
def send_signed_call(api_endpoint, params):
nonce = _nonce()
reqid = _reqid(nonce)
call = json.dumps({
"id" : reqid,
"nonce" : nonce,
"call" : api_endpoint,
"params" : params,
})
callByte = bytes(call, "utf-8")
sign = hmac.new(base64.b64decode(api_secret), callByte, hashlib.sha512).digest()
skey = bytes.fromhex(api_key.replace("-",""))
signedcall = skey + sign + callByte
return json.dumps({
"op" : "call",
"call" : base64.b64encode(signedcall).decode("utf-8"),
"id" : reqid,
"context" : "mtgox.com"
})
你们不知道我现在有多开心!
关于python - 在 Python 3 中对 MtGox WebSocket API 的身份验证调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18557453/
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 2年前关闭。 Improve thi
我无法通过名为 websocketpp 的 WebSockets C++ 库从 MtGox API 获取信息: #include #include #include typedef websoc
我在 Java 中使用 PubNub API 从 MtGox 中提取数据。 当检索数据时,API 以 JSONObject 的形式提供它,它表示 JSON 数据的树结构。尝试直接从 JSONObjec
我正在使用带有socket.io-client的nodejs来连接到此处描述的MtGox Streaming API:https://en.bitcoin.it/wiki/MtGox/API/Stre
考虑到数据的公共(public)性质,这应该很简单,但出于某种原因,我的 Ruby 脚本在发出请求时超时。 网址是http://mtgox.com/api/1/BTCGBP/ticker - 在浏览器
下面的代码似乎没有做任何事情。有退订的方法吗? var MTGOX = 'https://socketio.mtgox.com/mtgox'; var conn = io.connect(MTGOX
这个问题在这里已经有了答案: Convenient way to extract data from the MtGox/PubNub JSON API? (2 个答案) 关闭 9 年前。 我在 J
我正在尝试使用 MtGox.com WebSocket API 进行身份验证,并在很长一段时间后设法完成了 JSON 数据所需的“调用”属性。然而,我意识到我正在使用 Python 2 来运行我的代码
我是一名优秀的程序员,十分优秀!