Correct path for firestore database in r - r

Hi and thanks for reading me. I'm trying to connect to a database in firestore (firebase) following the steps of a tutorial I found on the internet, but I got a problem right now, and that is when trying to write the database path for the function , then it returns me an empty JSON. It does not give me any error message, so I assume that the connection to the firestore works, the problem arises when writing the path to the database.
The functions to connect to firestore are the following:
library(jsonlite)
library(httr)
sign.up <- function(email, password, api_key) {
#https://firebase.google.com/docs/reference/rest/auth/#section-create-email-password
r <- POST(paste0("https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=", api_key),
add_headers("Content-Type" = "application/json"),
body = toJSON(list(email = email, password = password, returnSecureToken = TRUE),auto_unbox=TRUE))
return(content(r))
}
sign.in <- function(email, password, api_key) {
#https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password
r <- POST(paste0("https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=", api_key),
add_headers("Content-Type" = "application/json"),
body = toJSON(list(email = email, password = password, returnSecureToken = TRUE),auto_unbox=TRUE))
return(content(r))
}
my_auth_info<-sign.in(
Sys.getenv("mail"),
Sys.getenv("password"),
Sys.getenv("token")
)
And the function to get the firestore data are the following:
read.db <- function(db_endpoint, auth_token) {
r <- GET(sprintf("https://firestore.googleapis.com/v1beta1/%s", db_endpoint),
add_headers("Content-Type" = "application/json",
"Authorization" = paste("Bearer", auth_token)))
return(r)
}
When i run the function with the following information I got an empty JSON:
read.db(
"projects/projectname/databases/(default)/documents/databasename",
my_auth_info$idToken
)
For the project name I use the following:
And for database name im using the following:
I need to use another information for the database path?
Thanks for the help

Related

Paginated API - R

With a paginated oAuth2.0 API, how do I use the response headers to view the next page?
So far I have successfully created an endpoint, app, token, and response:
# here's the key and username
API_KEY = 'my key'
API_USER = 'my username'
# set up the endpoint
api_endpoint= httr::oauth_endpoint(
authorize = "https://www.hydrovu.com/public-api/oauth/authorize",
access = "https://www.hydrovu.com/public-api/oauth/token"
)
# heres the app we will use to download data to
App = httr::oauth_app("xxx", key = API_USER, secret = API_KEY)
# heres the token we will use as a password
API.token = httr::oauth2.0_token(api_endpoint,
App,
scope = "https://www.hydrovu.com/public-api/oauth/token",
client_credentials=TRUE,
cache = FALSE,
config_init = user_agent("Testing Oauth with httr"))
# this is the response or the data
res <- httr::GET(url = "https://www.hydrovu.com/public-api/v1/locations/{id}/data?startTime=0",
user_agent("Testing Oauth with httr"),
config(token = API.token))
The response headers from res include next-page, which has a long string as the value. E.g. 6haGnbeLO09jlwe6poy7erT43qp
How to I use this next-page information to actually get the next page?
I am using the HydroVu API: https://www.hydrovu.com/public-api/docs/index.html
Let me know if you need more information!
After speaking to the excellent Hadley Wickham from httr maintenance, I have found the solution.
I was originally trying to pass x-isi-next-page to x-isi-next-page through add_headers() like so:
# original GET
res <- httr::GET(url,
user_agent("Testing Oauth with httr"),
config(token = API.token))
# write x-isi-next-page to object
nextpage = res$headers$`x-isi-next-page`
# pass to add_headers()
res <- httr::GET(url,
user_agent("Testing Oauth with httr"),
config(token = API.token),
add_headers(`x-isi-next-page` = nextpage))
When in fact I should have been passing it to x-isi-start-page
resp <- httr::GET(url,
user_agent("Testing Oauth with httr"),
config(token = API.token),
add_headers(`x-isi-start-page` = nextpage))
Thus, giving me the next page of results.
Now, to decode the json and create a compiled csv...

Getting error when trying to write to firebase (firestore) with R

I am trying to use the package Kohze/fireData to upload and download .txt documents to/from Firebase.
The package can be found here: https://github.com/Kohze/fireData
The function descriptions can be found here: https://github.com/Kohze/fireData/blob/master/R/index.R
When I upload a file the response should be “directory/filename”, but when I try to upload a file I get a response of “directory/“, and am not able to locate or download the file, so I think that my files are not uploading properly. Do you have any idea why this would be the case and how I could fix it?
I am getting an error saying Error 404
redirect_uri_mismatch
Here is the code
upload <- function(x, projectURL, directory = “main”, token = “none”){
output = fileConversion(x)
if (token == “none”) {
Response = httr::POST(paste0(projectURL,“/”,directory,“.json”), body = jsonlite::toJSON(output, auto_unbox = TRUE))
} else {
Response = httr::POST(paste0(projectURL,“/”,directory,“.json?auth=“,token), body = jsonlite::toJSON(output, auto_unbox = TRUE))
}
return(paste0(directory,“/”,httr::content(Response)$name))
}
download <- function(projectURL, fileName, secretKey = “none”, token = “none”, isClass = FALSE) {
if (secretKey == “none” && token == “none”) {
urlPath = paste0(projectURL,“/”,fileName,“.json”)
} else if (token != “none”) {
urlPath = paste0(projectURL,“/”,fileName,“.json?auth=“,token)
} else {
urlPath = paste0(projectURL,“/”,fileName,“.json?auth=“,secretKey) }
data = httr::GET(urlPath)
if (is.null(jsonlite::fromJSON(httr::content(data,“text”))))
warning(“No data found at database location.“)
if (isClass) {
retrievedData = httr::content(data,“text”)
tempPath = tempfile()
writeBin(jsonlite::base64_dec(jsonlite::fromJSON(retrievedData)), tempPath)
return(readRDS(tempPath))
} else {
return(jsonlite::fromJSON(httr::content(data,“text”))) }
}
From what it sounds like, you are not using the correct R package. The Kohze/fireData package is used for uploading the firebase realtime database, not the firebase storage. For uploading text files to the firebase storage, you'd need to use the paulsp94/fireData package (kinda confusing as both are fireData haha). The package can be found at https://github.com/paulsp94/fireData

How to connect to Slack via slackr package in R

I'm trying to set up the slackr package in R to allow me to post to Slack through RStudio, but it seems the connection isn't working.
I've installed the package, set up the required App in my Slack Workspace, have a webhook and access tokens, manually granted all of Scopes needed in Slack, set up the config file and have confirmed via the cmd line that this works by sending a message to the channel through that successfully. Unfortunately nothing is working via R.
slackr_msg(paste("test"),
,channel = "#it-reports")
slackr_ims(api_token="xoxb-...")
When run the above both produce Error: Join columns must be present in data. x Problem with 'id'. which I can't find anybody else experiencing. It sounds like an issue with the user, but the app is in the channel, has permission to access all public channels, and this error appears regardless of whether I try and send it via my logged in account or just from the bot. When I just try and return something simple like listed channels with slackr_channels I get NULL. I've tried both OAuth Access Token and Bot User OAuth Access Token with all permissions needed in the scope but neither are letting me access Slack.
I'm not sure you still need an answer, but in case anyone else is looking - I've run into the same issue. I think the underlying method that your slack channel name "#my_excellent_named_channel" is converted to the channel id is deprecated.
"https://slack.com/api/channels.list" has been changed to "https://slack.com/api/conversations.list" in the methods.
I couldn't find a better way than brute forcing my channel id because I only need to publish to a notifications channel.
nextCursor=""
for (j in c(1:10)){
resp<-GET(url = "https://slack.com/api/conversations.list",
query = list(token=YOUR_BOT_TOKEN,
cursor=nextCursor
))
foo=fromJSON(content(resp, as="text"))
for (i in c(1:100)){
if(foo$channels[[i]]$name %like% 'my_excellent_named_channel'){
print(paste(nextCursor, "\n"))
print(paste(i,foo$channels[[i]]$name,"\n"))
print(paste(foo$channels[[i]]$id))
}
}
nextCursor=foo$response_metadata[[1]]
}
You'll probably need to run through a few more than the 10 top level iterations, but if you go too fast it locks you out. I then just exchanged the channel name in my config file for the channel id, and rewrote the functions to avoid the conversion.
Here's slackMsg (conversion of slackrMsg)
slackMsg=function (txt = "", channel = Sys.getenv("SLACK_CHANNEL"), username = Sys.getenv("SLACK_USERNAME"),
icon_emoji = Sys.getenv("SLACK_ICON_EMOJI"), api_token = Sys.getenv("SLACK_API_TOKEN"),
...)
{
if (api_token == "") {
stop("No token specified. Did you forget to call slackr_setup()?",
call. = FALSE)
}
if (icon_emoji != "") {
icon_emoji <- sprintf(", \"icon_emoji\": \"%s\"", icon_emoji)
}
output <- paste0(txt, collapse = "\n\n")
loc <- Sys.getlocale("LC_CTYPE")
Sys.setlocale("LC_CTYPE", "C")
on.exit(Sys.setlocale("LC_CTYPE", loc))
resp <- POST(url = "https://slack.com/api/chat.postMessage",
body = list(token = api_token, channel = channel,
username = username, icon_emoji = icon_emoji, text = output,
as_user = TRUE, link_names = 1, ...))
warn_for_status(resp)
return(invisible())
}
Don't forget to give your bot chat.write permissions in your Slack App.

Using Rtweet's search_fullarchive in R

I am trying to use rtweet's search_fullarchive using my premium token registered at Twitter Developer. However, I got an error message of:
Warning: list(message = "Forbidden: Authentication succeeded but account is not authorized to access this resource.", sent = "2019-07-14T14:30:11+00:00", transactionId = "xxxxxxxxxx")
How do I get past this problem? Is there an error in the way i code?
I ensured my token is working well by testing it using httr's POST method and it worked completely fine.
I also tested my token using normal search_tweets, and also worked fine.
cons_key = "xxx"
cons_sec = "xxx"
acc_tok = "xxx"
acc_sec = "xxx"
app = "abc"
token = rtweet::create_token(app,cons_key,cons_sec,acc_tok,acc_sec)
manutd = search_fullarchive("manchester united",n=500,
fromDate = "201812010000",toDate = "201902010000",
env_name = app,token = token)
I expected a tibble dataframe as usually Rtweet's returns. However this is what i received:
"Warning: list(message = "Forbidden: Authentication succeeded but account is not authorized to access this resource.", sent = "2019-07-14T14:30:11+00:00", transactionId = "xxxxxxxxxx")"
data frame with 0 columns and 0 rows
To help anyone who might have trouble with the same thing, i'll write down a bit of explanation. Firstly, if you have registered a premium account, go to
To create the token, you need the App Name.
token = rtweet::create_token(app_name,cons_key,cons_sec,acc_tok,acc_sec)
To do query, you need the environment name.
search_fullarchive(q, n = 100, fromDate = NULL, toDate = NULL, env_name = "dev_name", safedir = NULL, parse = TRUE, token = token)
Hope it helps beginners or anyone who might have the same troubles.

How to fetch Shopify store orders using Shopify's API

I'm struggling to import the orders for a Shopify development store using the httr package in R. Here's what I've tried.
I've created a development store and made some fake orders.
Within my development store, I added a private app and generated my API key and Password
Following this article, I tried implementing the following request
Code
apikey <- "foo"
pass <- "bar"
shop <- GET(
url = "my-test-store.myshopify.com/orders.json",
authenticate(user = apikey, password = pass)
)
But this gives a 401 status code. However, this works but returns xml instead of json
shop <- GET(
url = "my-test-store.myshopify.com/orders",
authenticate(user = apikey, password = pass)
)
How can I retrieve the results as JSON instead of XML?
Note that I can also fetch the orders using the R package shopifyr but would rather not use that package as it is no longer maintained.
You're close. Try this:
library(httr)
apikey <- "foo"
pass <- "bar"
orders <- GET(
url = "https://yourshop.myshopify.com/admin/orders.json",
authenticate(user = apikey, password = pass)
)
content(orders)
Update 2019-05-13
I created an R package called shopr for querying data via the Shopify API. Fetching orders looks like this
library(shopr)
shopr_get_orders(
shopURL = "https://my-test-store.myshopify.com",
APIKey = "abc123",
APIPassword = "def456"
)
Old Answer
Figured it out.
orders <- GET(
url = "https://my-test-store.myshopify.com/admin/orders",
add_headers(Accept = "application/json"),
authenticate(user = apikey, password = pass)
)
orders
The trick was to explicitly put "https://..." in the url otherwise httr was prepending "http://" to the url, causing my 401 problem.

Resources