【发布时间】:2019-01-09 19:36:14
【问题描述】:
我正在尝试向 LocalBitcoins API 服务器发送 POST 请求,但我只收到错误(41 和 43),尽管 GET 请求即使使用参数(参数)也能正常工作。
Sub initrequest()
'this is a POST request with no arguments that results in error
JsonResponse = PrivateLocalBTC("POST", "/api/notifications/mark_as_read/9b9c2b5a16a3/")
'this is a GET request, with "ads=.." parameter, that works
'JsonResponse = PrivateLocalBTC("GET", "/api/ad-get/", "ads=771318")
Debug.Print JsonResponse
End Sub
还有 HTTP 请求子:
Function PrivateLocalBTC(Method As String, endpoint As String, Optional params As String) As String
Dim NonceUnique As String
NonceUnique = CreateNonce(13)
TradeApiSite = "https://localbitcoins.com"
apikey = "..............."
secretkey = "............"
Message = NonceUnique & apikey & endpoint & params
apisign = ComputeHash_C("SHA256", Message, secretkey, "STRHEX")
If params <> "" Then urlparams = "?" & params
Url = TradeApiSite & endpoint & urlparams
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open Method, Url, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "Apiauth-Key", apikey
objHTTP.setRequestHeader "Apiauth-Nonce", NonceUnique
objHTTP.setRequestHeader "Apiauth-Signature", apisign
objHTTP.Send ("")
objHTTP.waitForResponse
PrivateLocalBTC = objHTTP.ResponseText
Set objHTTP = Nothing
End Function
我尝试了很多变体,将端点、nonce、apikey 放在 .Send command 、urlencoding 的请求正文中,但没有得到肯定的结果。
ComputeHash_C、CreateNonce 是独立的函数。该代码的灵感来自 https://github.com/krijnsent/crypto_vba 的伟大共享工作!
LocalBitcoins API 文档在这里:https://localbitcoins.com/api-docs/
【问题讨论】:
标签: excel vba api authentication winhttprequest