2013-04-06 161 views
1

我試圖通過MT GOX API請求一些數據(mtgox.com)和theres在python一些示例代碼,我想基本上覆制到R.URL請求,蟒蛇至R翻譯請

import hmac, base64, hashlib, urllib2 
base = 'https://data.mtgox.com/api/2/' 

def makereq(key, secret, path, data): 
hash_data = path + chr(0) + data 
secret = base64.b64decode(secret) 
sha512 = hashlib.sha512 
hmac = str(hmac.new(secret, hash_data, sha512)) 

header = { 
    'User-Agent': 'My-First-Trade-Bot', 
    'Rest-Key': key, 
    'Rest-Sign': base64.b64encode(hmac), 
    'Accept-encoding': 'GZIP', 
} 

return urllib2.Request(base + path, data, header) 

我有一些R代碼裏面已經

install.packages("base64") 
install.packages("caTools") 
install.packages("digest") 
install.packages("RCurl") 
library(RCurl) 
library(caTools) 
library(base64) 
base<- "https://data.mtgox.com/api/2" 
path<- "BTCUSD/money/ticker" 
APIkey<-"******" #this is private but its a long hex number 
secretAPIkey<-"*****" #this too, but this is in base64 


makeReq<-function(key, secret, path, post_data) 
{ 
    browser() 
    message <- paste(path, NULL, post_data) 
    secret<-base64decode(secret,"character") 
    theHmac <-hmac(secret,message,"sha512") 
    header <- 
    { 
    c(
    User.Agent = "My Bot", 
    Rest.Key = key, 
    Rest.Sign = base64encode(theHmac), 
    Acccept.encoding = "GZIP" 
    ) 
    } 
    return (getURL(paste(base,path), post_data, header)) 
} 

我不知道如何讓「頭」的事情工作,雖然,我可能是()不正確地使用的getURL。 如果你想看到整個問題,說明在這裏https://bitbucket.org/nitrous/mtgox-api/overview,向下滾動到第一個代碼塊。

,但我可能只是讓有R頭部語法一些基本的錯誤...

+0

人?反饋如果這是一個壞問題...? – hedgedandlevered 2013-04-06 23:40:15

+0

試試這個(未經測試):'getURL(粘貼(base,path),post_data,httpheader = header)' – 2013-04-07 01:12:09

+1

粘貼的默認值是'sep =「」'。所以'粘貼(基礎,路徑)'是製作一個空格的字符串。 (URL = url,writefunction = writeFun,...,.opts = .opts)錯誤: 未命名的捲曲選項:nonce = – kmm 2013-04-07 02:55:24

回答

0

嘗試使用postForm(從RCurl),而不是使用getURL:

postForm(paste(base,path), 
    .opts = list(postfields = post_data, 
       useragent = 'R', 
       httpheader = c('Rest-Key' = key, 
           'Rest-Sign' = base64encode(theHmac)), 
       timeout = 4, 
       ssl.verifypeer = FALSE) 
     )