请求URL请求,python到R翻译

时间:2013-04-06 22:37:36

标签: r

我正在尝试通过mt gox API(mtgox.com)请求一些数据,而python中的一些示例代码我基本上要复制到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头语法犯了一些基本错误...

1 个答案:

答案 0 :(得分: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)
         )