How to use a proxy api in R - r

I'm using a proxy service IP in R which I have configured perfectly by the following:
https://support.rstudio.com/hc/en-us/articles/200488488-Configuring-R-to-Use-an-HTTP-or-HTTPS-Proxy
This service, which im not sure if I can mention here, also offers a proxy API.
e.g. https://xxxx.com/proxy-api/7976ed1223443d907283443dffc961ff2c9bb219_993353-2033906
I'm using the proxy service for a read_html script from library(rvest).
Would it be possible, if more efficient, to use proxy API, rather than modifying the Renviron.site file?

solved it as follows:
library(jsonlite)
json_file <- "https://xxxx.com/proxy-api/7976ed1223443d907283443dffc961ff2c9bb219_993353-2033906"
json_data <- fromJSON(json_file, flatten=TRUE)
x<-sample(1:5, 1)
Sys.setenv(http_proxy = json_data[x])
Sys.setenv(https_proxy = json_data[x])

Related

API GET call in R using filter from OData

I am trying to download data in R via an API using OData protocol. I am new to APIs so please bear with me.
I am trying to send a GET command using the following from the API specs:
https://address/v1/meter?$filter=GroupGUID eq guid’ID’
where address is replaced by the URL and ID is replaced by the ID of the meter.
The API uses basic authorization, so I am using the following code from the httr library:
MyUrl <- "https://address/v1/meter?$filter=GroupGUID eq guid'ID'"
MyData <- GET(MyUrl, authenticate("Username", "access code", "basic"))
This gives an status code 400 - The request is badly formed
The authentication works, when I am not using the $filter command.
I have been in touch with the developers of the API and they have confirmed that my GET command is correct. But they are not familiar with R, so they cannot help. I suspect that it may be the space before and after “eq” that is causing the problem.
Can anyone help or point me to a description of using the Odata protocol in R?
Best regards, Rune
I finally figured it out. If I replace space " " with "%20", it works.
This solution is also described her: passing odata $filter in httr GET request

post in r using access token in header

I have to post some JSON to a restful endpoint and decided to do this in R. I am aware of the httr library. However, I am not sure how to add an access token for Azure active directory. This is some code:
library(httr)
access_token <- "SomeSecretStuffeyJ0eXAiOiJKV"
json <- '{"test_data":"hello_world"}'
uri <- "https://somewhere.net/bla"
result <- POST(
uri
, content_type_json()
, body = json
)
I am not sure if I can do this using httr? Could someone please help out with code and/or suggest alternative library? Thanks!

Suggestions needed for building R server REST API's that I can call from external app?

I've seen lots of articles about consuming data in R from other RESTful API services, but I have really struggled to find any articles about the reverse. I'm interested in R being the server, and not the client. I'd like a Node.js app to call a RESTful API of an R-server so I can leverage specific analytical functions such as multi-seasonality forecasting. Anyone have any ideas?
You can use httpuv to fire up a basic server then handle the GET/POST requests. The following isn't "REST" per se, but it should provide the basic framework:
library(httpuv)
library(RCurl)
library(httr)
app <- list(call=function(req) {
query <- req$QUERY_STRING
qs <- httr:::parse_query(gsub("^\\?", "", query))
status <- 200L
headers <- list('Content-Type' = 'text/html')
if (!is.character(query) || identical(query, "")) {
body <- "\r\n<html><body></body></html>"
} else {
body <- sprintf("\r\n<html><body>a=%s</body></html>", qs$a)
}
ret <- list(status=status,
headers=headers,
body=body)
return(ret)
})
message("Starting server...")
server <- startServer("127.0.0.1", 8000, app=app)
on.exit(stopServer(server))
while(TRUE) {
service()
Sys.sleep(0.001)
}
stopServer(server)
I have the httr and RCurl packages in there since you'll probably end up needing to use some bits of both to parse/format/etc requests & responses.
node-rio provides a way to talk to rserve (a TCP/IP server that allows the use of R functions) from node.js.
Here is an example of use (from the documentation):
var rio = require('rio');
rio.evaluate("as.character('Hello World')");

OAuth2 GitHub API token

I have a Client ID and Client Secret after having set up an application in github, I'm not sure what the URL or the callback URL is meant to be for that...which i think is causing me problems
I also have a private repo that I would like the application to access...
The way I would like to access the private repo would be via R, so I have found some packages that might help including ROAuth and oauth, but I'm not too sure how to go about using these to get the tokens, as they tend to require a bunch of URLs to make the requests from, and I am unsure as to what the URLs are to get these requests for tokens.
Looking at http://developer.github.com/v3/oauth/ doesn't seem to be amazingly helpful in terms of my inputs for oauth or Oauth2Authorize functions for each of the respective packages.
The end goal is to source files from the private repo, since source_url('private.repo.file.url') doesn't work
I tried the basic authentication using curl through bash, but wasn't able to find a token.
Any walkthrough examples of how to go about doing this would be greatly appreciated.
P.S. this is a follow up question from r sourcing private repos from github
You just need to create an oAuth token at https://github.com/settings/tokens
and get required file via GitHub API using code like below
library(RCurl)
library(devtools)
jsonRawFile <- fromJSON(getURL("https://api.github.com/repos/USERNAME/REPONAME/contents/filename.R",
httpheader = c(Authorization = "token 38ebb0393fe1757ffde9c45d81adzzzzzzzzz",
"User-Agent" = "RCurl"),
.opts = list(ssl.verifypeer = FALSE)))
source_url(jsonRawFile$download_url)
The format of Authorization header should be strictly "token " + your_token_from_account.

How to sign R API requests with credentials

I'm trying to use Hackpad's API in R. Unlike the example in httr, the Hackpad documentation
1. uses oauth 1.0 and
2. says that the requests should be signed with your credentials.
I do not completely understand how to do the 2nd part in R (with any library). I understand how to use the request, authorize, and access URLs -- but those are not provided for Hackpad (as far as I can tell).
Here is what I tried in R (to no success):
library(httr)
myapp <- oauth_app("hackpad",
key = mykey,
secret = mysecret)
hackpad_token <- oauth1.0_token(oauth_endpoint(authorize = 'https://hackpad.com/api/1.0/',
request = 'https://hackpad.com/api/1.0/',
access = 'https://hackpad.com/api/1.0/'),
myapp)
and also
req <- GET("https://hackpad.com/api/1.0/pad/k9bpcEeOo2Q/content/latest.html",
config = authenticate(user=myuser,
password=mypass))
which tries to return the API documentation content.
Thank you for your help

Resources