将openpath.cc API与R一起使用

将openpath.cc API与R一起使用,r,api,oauth,httr,R,Api,Oauth,Httr,我试图从中提取我的位置数据,以便与R一起使用。 API使用OAuth并有文档记录,但是,它只提供了Python中的一个示例 在研究了如何在R中处理OAuth(我几乎不熟悉)之后,我发现,因此我使用了提供的使用示例作为基础 根据API文档,所有请求的端点都是https://openpaths.cc/api/1,我有我的访问密钥和访问密码,所以我天真地将它们插入了cKey,cSecret,reqURL,accessURL,authURL,和testURL,但只收到了“坏请求”由于凭证$handsha

我试图从中提取我的位置数据,以便与R一起使用。 API使用OAuth并有文档记录,但是,它只提供了Python中的一个示例

在研究了如何在R中处理OAuth(我几乎不熟悉)之后,我发现,因此我使用了提供的使用示例作为基础

根据API文档,所有请求的端点都是
https://openpaths.cc/api/1
,我有我的访问密钥和访问密码,所以我天真地将它们插入了
cKey
cSecret
reqURL
accessURL
authURL
,和
testURL
,但只收到了“坏请求”由于
凭证$handshake()
行的原因

reqURL <- "https://openpaths.cc/api/1"
accessURL <- "https://openpaths.cc/api/1"
authURL <- "https://openpaths.cc/api/1"
cKey <- "key"
cSecret <- "secret"
testURL <- "https://openpaths.cc/api/1"
credentials <- OAuthFactory$new(consumerKey=cKey,
                                consumerSecret=cSecret,
                                requestURL=reqURL,
                                accessURL=accessURL,
                                authURL=authURL,
                                needsVerifier=TRUE)
credentials$handshake()
## the GET isn’t strictly necessary as that’s the default
credentials$OAuthRequest(testURL, "GET")

reqURL这里是我能找到的。我收到“400未授权”,可能是因为我的OpenPath帐户未连接到foursquare,可能是代码有问题。请试一试

所需套餐:

library(RCurl)
library(digest)
library(base64)
一些借用/改编自ROAuth的功能:

## Get a random sequence of characters.
## Nonce - number used only once.
genNonce <- function(len = 15L + sample(1:16, 1L)) {
  els <- c(letters, LETTERS, 0:9, "_")
  paste(sample(els, len, replace = TRUE), collapse = "")
}

## this function is derived from utils::URLencode
## Characters not in the unreserved character set ([RFC3986] section 2.3) MUST be encoded
##   unreserved = ALPHA, DIGIT, '-', '.', '_', '~'
## cf. http://oauth.net/core/1.0/#encoding_parameters
encodeURI <- function(URI, ...) {
  if (!is.character(URI)) {
    URI
  } else {
    OK <- "[^-A-Za-z0-9_.~]"
    x <- strsplit(URI, "")[[1L]]
    z <- grep(OK, x)
    if (length(z)) {
      y <- sapply(x[z], function(x) paste("%", toupper(as.character(charToRaw(x))),
                                          sep = "", collapse = ""))
      x[z] <- y
    }
      paste(x, collapse = "")
  }
}

## we escape the values of the parameters in a special way that escapes
## the resulting % prefix in the escaped characters, e.g. %20 becomes
## %2520 as %25 is the escape for %
## cf. http://tools.ietf.org/html/rfc5849#section-3.4.1.3.2
normalizeParams <- function(params, escapeFun) {
  names(params) <- sapply(names(params), escapeFun, post.amp = TRUE)
  params <- sapply(params, escapeFun, post.amp = TRUE)
  ## If two or more parameters share the same name, they are sorted by their value.
  params <- params[order(names(params), params)]
  return(paste(names(params), params, sep = "=", collapse = "&"))
}

## From Ozaki Toru's code at https://gist.github.com/586468
signWithHMAC <- function(key, data) {
  blockSize <- 64
  hashlength <- 20
  innerpad   <- rawToBits(as.raw(rep(0x36, blockSize)))
  outerpad   <- rawToBits(as.raw(rep(0x5C, blockSize)))
  zero       <- rep(0 ,64)

  HexdigestToDigest <- function(digest) {
    as.raw(strtoi(substring(digest, (1:hashlength)*2-1,
                            (1:hashlength)*2), base=16))
  }

  mac <- function(pad, text) {
    HexdigestToDigest(digest(append(packBits(xor(key, pad)), text),
                             algo='sha1', serialize=FALSE))
  }

  if(nchar(key) >= 64) {
    keyDigested <- digest(key, algo="sha1", serialize=FALSE)
    key <- intToUtf8(strtoi(HexdigestToDigest(keyDigested), base=16))
  }
  key <- rawToBits(as.raw(append(utf8ToInt(key), zero)[1:blockSize]))

  base64(mac(outerpad, mac(innerpad, charToRaw(data))))[1]
}

## Sign an request made up of the URL, the parameters as a named character
## vector the consumer key and secret and the token and token secret.
signRequest  <- function(uri, consumerKey, consumerSecret, params=character(), 
                         oauthKey = "", oauthSecret = "", httpMethod = "GET",
                         nonce = genNonce(),
                         timestamp = Sys.time()) {
  httpMethod <- toupper(httpMethod)

  params["oauth_nonce"] <- nonce
  params["oauth_timestamp"] <- as.integer(timestamp)
  params["oauth_consumer_key"] <- consumerKey
  params["oauth_signature_method"] <- 'HMAC-SHA1'
  params["oauth_version"] <- '1.0'
  if(oauthKey != "") params["oauth_token"] <- oauthKey
  odat <- paste(
      encodeURI(httpMethod), encodeURI(uri), 
      encodeURI(normalizeParams(params, encodeURI), post.amp = TRUE),
      sep = "&"
  )
  okey <- encodeURI(consumerSecret)
  if(oauthSecret != "") okey <- paste(okey, encodeURI(oauthSecret), sep = "&")

  params["oauth_signature"] <- signWithHMAC(okey, odat)
  return(params)
}
##获取随机字符序列。
##Nonce-仅使用一次的编号。

genNonce我在这个问题上取得了一些进展,尽管这是一个挑战 对于站点的片状,以及自定义OAuth进程 使用。首先,您需要安装httr的开发版本-此 导出一些以前的内部函数

devtools::install_github("hadley/httr")
OpenPath的与众不同之处在于,应用程序的机密和密钥与 令牌和令牌机密。这意味着我们需要编写一个自定义身份验证 标题:

库(httr)
使用存储在环境变量OpenPath\u CONSUMER\u secret中的secret的应用程序
#实现两段式身份验证的自定义头和oauth_body_散列

auth_header问题是什么?您是在具体解决了错误的握手请求之后吗?或者你在问自己是否正确使用了ROAuth?或多或少都是这样。我的主要问题是,首先我不知道如何处理openpaths API,除了python示例之外,我找不到任何关于它的文档,因此我不知道我在那里使用ROAuth做的事情是否有意义。不幸的是,OpenPath在这里似乎没有标签,我的声誉太低,无法创建一个。有趣的问题是,我也无法让它工作。请注意,
httr
包包含一些oauth示例,这些示例可能比
ROAuth
更容易适应此问题。一旦OpenPath再次启动,我将为您提供一个httr示例。你的设置肯定有问题,因为reqURL等应该是不同的。啊,这使用了两条腿的OAuth,这有点不寻常。我要花的时间比预期的要长一些。嗯,当我为你的函数编写源代码时,插入我的键并尝试
测试你的
RCurl
1.95-4.1
——我刚才还检查了包更新(我正在使用Rstudio),但没有针对RCurl的更新。在过去的几个小时里,我尝试了几次,但除了偶尔超时之外,我只收到了
XML内容似乎不是XML:“400:未授权”
作为回答。我有点被API的明显奇怪之处激怒了,因为该网站声明它的使用非常直接。至于脆弱性——我不记得网站以前是脆弱的,这似乎是(不幸的是)最近的问题。
devtools::install_github("hadley/httr")
library(httr)

app <- oauth_app("OpenPaths", "JSLEKAPZIMFVFROHBDT4KNBVSI")
#> Using secret stored in environment variable OPENPATHS_CONSUMER_SECRET

# Implement custom header for 2-leg authentication, and oauth_body_hash 
auth_header <- function(url, method = "GET") {
  oauth_signature(url, method, app, app$key, app$secret,
    # Use sha1 of empty string since http request body is empty
    body_hash = "da39a3ee5e6b4b0d3255bfef95601890afd80709")  
}
url <- "https://openpaths.cc/api/1"
r <- GET(url, oauth_header(auth_header(url)))
stop_for_status(r)
content(r)