簡體   English   中英

需要幫助在 R 中使用 FIXML 和 ROAuth 發出 POST 請求

[英]Need help making a POST request in R using FIXML and ROAuth

我正在嘗試連接到交易 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 或 text 並且內容類型應該在您的請求中反映出來,這樣它就不會嘗試對其進行編碼。 您應該嘗試 Content-Type: text/xml 而不是 Content-Type:application/x-www-form-urlencoded。 這里(粘貼在下面)是我們從您的請求中得到的:

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

我將如何在代碼中解決這個問題?

我認為您需要像這樣將 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",)

或者你有沒有找到更好的解決方案?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM