1

我正在尝试通过 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 标头语法时犯了一些基本错误......

4

1 回答 1

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)
         )
于 2013-05-27T23:01:14.037 回答