more api stuff

This commit is contained in:
2014-11-29 16:11:13 +01:00
parent a1bb0d8c4a
commit 7723ceca8e
6 changed files with 171 additions and 3 deletions
+69
View File
@@ -0,0 +1,69 @@
# calculates the signature for the request as described in the docs:
# https://dev.twitter.com/docs/auth/creating-signature
#' Generates OAuth1 signature needed to authorize API request
#'
#' @param method f.x. "HMAC-SHA1"
#' @param url f.x. "https://api.twitter.com/1.1/friends/ids.json"
#' @param api f.x. c(cursor=-1, screen_name="hrw", count=10)
#' @param params named vector needed for generating oauth1 signature
#' @return signature string
oauth1_signature <- function(method, url, api, params, test=FALSE) {
library(RCurl);
library(digest);
library(base64enc);
# http://oauth.net/core/1.0/#encoding_parameters
if(curlEscape(".") == ".") {
# that's how we want it!
} else if(curlEscape(".") == "%2E") {
curlEscape <- function(str) {
esc <- RCurl::curlEscape(str)
esc <- gsub("%2E",".",esc)
esc <- gsub("%2D","-",esc)
esc <- gsub("%5F","_",esc)
esc <- gsub("%7E","~",esc)
}
} else {
stop("curlEscape('.') is supposed to be either '.' or '%2E'")
}
secrets <- params[c("consumer_secret","oauth_token_secret")];
# exclude *_secret
params <- params[! names(params) %in% c("consumer_secret","oauth_token_secret")]
params <- c(api, params);
params_sorted <- params[sort(names(params))];
params_escaped <- sapply(params_sorted, curlEscape);
pstr <- paste(paste(names(params_escaped),"=",params_escaped,sep=""),collapse="&");
# it is important to use curlEscape instead of URLencode because the latter encodes
# using lower case letters but we need upper case letters. Otherwise the resulting
# signature will be different from what Twitter/OAuth expects/calculates.
final <- sprintf("%s&%s&%s",toupper(method), curlEscape(url), curlEscape(pstr));
sig <- sprintf("%s&%s",curlEscape(secrets["consumer_secret"]),curlEscape(secrets["oauth_token_secret"]));
hmac <- hmac(sig,final,algo="sha1",raw=TRUE)
signature <- base64encode(hmac);
signature_escaped <- curlEscape(signature);
if(test) {
return(
list(
params_escaped = params_escaped,
pstr = pstr,
final = final,
sig = sig,
hmac = hmac,
signature = signature,
signature_escaped = signature_escaped
)
)
}
return(signature_escaped);
}
+73
View File
@@ -0,0 +1,73 @@
#' GET requests Twitter API
#'
#' @param url f.x. "https://api.twitter.com/1.1/friends/ids.json"
#' @param api f.x. c(cursor=-1, screen_name="hrw", count=10)
#' @param params named vector needed for generating oauth1 signature
#' @param print_result TRUE if you want to have resulting JSON printed out
#' @param use_cygwin TRUE if you would like to resort to Cygwin/Curl workaround
#' @param cygwin_bash f.x. "c:\\cygwin64\\bin\\bash.exe"
#' @return JSON
#' @export
twitter_api_call <- function(
url, api, params, print_result=FALSE,
use_cygwin=FALSE, cygwin_bash="c:\\cygwin64\\bin\\bash.exe", print_cmd=FALSE,
test=FALSE) {
library(jsonlite);
library(RCurl);
if(is.na(params["oauth_timestamp"])) {
params["oauth_timestamp"] <- as.character(as.integer(Sys.time()));
}
if(is.na(params["oauth_nonce"])) {
params["oauth_nonce"] <- sprintf("%d%s",as.integer(Sys.time()),paste(floor(runif(6)*10^6),collapse=""));
}
if(test) {
test_data <- oauth1_signature(method = "GET", url, api, params, test=TRUE);
params["oauth_signature"] <- test_data[["signature_escaped"]]
} else {
params["oauth_signature"] <- oauth1_signature(method = "GET", url, api, params, test=FALSE);
}
httpheader <- c(
"Authorization" = sprintf(paste(c(
'OAuth oauth_consumer_key="%s", oauth_nonce="%s", oauth_signature="%s", ',
'oauth_signature_method="%s", oauth_timestamp="%s", oauth_token="%s", oauth_version="1.0"'),
collapse=""),
params["oauth_consumer_key"], params["oauth_nonce"], params["oauth_signature"],
params["oauth_signature_method"], params["oauth_timestamp"], params["oauth_token"], params["oauth_version"])
);
q <- paste(paste(names(api),api,sep="="), collapse="&");
urlq <- paste(url,q,sep="?");
if(!test) {
if(!use_cygwin) {
result <- getURL(urlq, httpheader=httpheader);
} else {
httpheader_escaped <- sprintf("Authorization: %s",gsub('"','\"',httpheader["Authorization"]))
cmd <- sprintf("%s -c \"/usr/bin/curl --silent --get '%s' --data '%s' --header '%s'\"", cygwin_bash, url, q, httpheader_escaped)
if(print_cmd) {
cat(cmd)
}
result <- system(cmd, intern=TRUE)
}
} else {
result <- "{}"
}
if(print_result) {
cat(prettify(result));
}
if(test) {
test_data[["httpheader"]] <- httpheader
test_data[["q"]] <- q
test_data[["urlq"]] <- urlq
return(test_data)
}
return(result);
}