我正在尝试连接到交易API并且我已成功发出GET请求,但是当我尝试发出POST请求时,我不断收到来自API的错误消息。我搜索了互联网和论坛的答案,但所有类似的问题都没有得到解决。
这是我正在使用的R脚本:
# Call the required packages
library(ROAuth)
library(RJSONIO)
library(XML)
# Set your application keys
cKey <- 'xxxx'
cSecret <- 'xxxx'
oKey <- 'xxxx'
oSecret <- 'xxxx'
# Set the API endpoint
tkURL <- "https://api.tradeking.com/v1/accounts/xxxxxxxx/orders/preview.xml"
# Create the OAuth connection - this is straight from the ROAuth documentation on CRAN
credentials <- OAuthFactory$new(consumerKey = cKey,
consumerSecret = cSecret,
oauthKey = oKey,
oauthSecret = oSecret,
needsVerifier = FALSE,
signMethod='HMAC')
# Update the connection so the handshake is TRUE
credentials$handshakeComplete <- TRUE
# Create FIXML
doc <- newXMLDoc()
root <- newXMLNode('FIXML', namespaceDefinitions = 'http://www.fixprotocol.org/FIXML-5-0-SP2',
newXMLNode('Order', attrs=c(TmInForce='0', Typ='1', Side='1', Acct='xxxxxxxx'),
newXMLNode('Instrmt', attrs=c(SecTyp='CS', Sym='FAS')),
newXMLNode('OrdQty', attrs=c(Qty='1'))), doc=doc)
newFIXML = saveXML(doc, encoding='UTF-8')
newFIXML
# Post order
response <- credentials$OAuthRequest(tkURL, method = "POST", newFIXML)
response
当我运行这个时,我收到了这个回复:
"<response id=\"-3491729f:14e4f104ddc:7f50\">\n\t
<type>Error</type>\n\t
<name>ScriptFailure</name>\n\t
<description></description>\n\t
<path>/v1/accounts/xxxxxxxx/orders/preview.xml</path>\n</response>"
attr(,"Content-Type")
charset
"application/xml" "UTF-8"
我与API人谈过并提出了这些建议,但他说他无法帮助解决R代码问题:
我对R不熟悉,所以我可能不会给你很多帮助 代码部分。至于响应id,似乎标题是 在请求正文中与fixml一起错误地发送。 此外,fixml似乎是编码的,它不应该。这是更多 像xml或文本,内容类型应反映在你的 请求,以便它不会尝试编码它。代替 Content-Type:application / x-www-form-urlencoded,你应该试试 内容类型:text / xml。这里(贴在下面)就是我们从你那里得到的 请求:
Headers:
Host:api.tradeking.com Content-Length:668 Accept:*/* Content-Type:application/x-www-form-urlencoded
Body:
=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3CFIXML%20xmlns%3D%22http%3A%2F%2Fwww.fixprotocol.org%2FFIXML-5-0-SP2%22%3E%0A%20%20%3COrder%20TmInForce%3D%220%22%20Typ%3D%221%22%20Side%3D%221%22%20Acct%3D%22xxxxxxxx%22%3E%0A%20%20%20%20%3CInstrmt%20SecTyp%3D%22CS%22%20Sym%3D%22FAS%22%2F%3E%0A%20%20%20%20%3COrdQty%20Qty%3D%221%22%2F%3E%0A%20%20%3C%2FOrder%3E%0A%3C%2FFIXML%3E%0A&oauth_consumer_key=xxxx&oauth_nonce=xxxx &oauth_signature_method=HMAC-SHA1&oauth_timestamp=1435846001&oauth_token=xxxx&oauth_version=1.0&oauth_signature=xxxx%2xxxx%3D
我将如何在代码中修复此问题?
答案 0 :(得分:0)
我认为您需要将FIXML代码放在URL后面,如下所示:
tkURL <- "https://api.tradeking.com/v1/accounts/******/orders/preview.json"
xsfixml <- "<FIXML xmlns=\"http://www.fixprotocol.org/FIXML-5-0-SP2\"> <Order TmInForce=\"0\" Typ=\"2\" Side=\"1\" Px=\"8.5\" Acct=\"******\"> <Instrmt SecTyp=\"CS\" Sym=\"UNG\"/> <OrdQty Qty=\"1\"/> </Order> </FIXML>"
tkURLfixml <- paste(tkURL, xsfixml)
response <- credentials$OAuthRequest(tkURLfixml, method="POST",)
还是您找到了更好的解决方案?