I have been working in R to create a shiny app that uses googledrive
and googlesheets4
. Everything has been working great, I hooked up OAuth and everything. Now, I'm looking to help build in more automation to my app and this includes digging in to Google Forms and since I'm already using gargle
-based code, I'd like to stick with it.
Eventually, I'd like to be able to update a couple of a form's questions tailored to the specific use and user of the app so that they don't have to do this themselves. However, I have not been successful in making any API calls.
I have been testing my code in an interactive environment (not in a shiny app) first to make sure things work. I started by following the instructions from gargle
on downloading the discovery document for an API. I did have to make a couple slight modifications to their code but I did successfully get a discovery document.
I then use the following
library(googledrive)
library(gargle)
fid <- "form_id"
# my slightly modified function to remove googledrive specific pieces
my_request_generate <- function(endpoint = character(),
params = list(),
key = NULL,
token = drive_token()) {
.endpoints <- readRDS("app/endpoints.RData") # my saved file with the endpoints for the API
ept <- .endpoints[[endpoint]]
if (is.null(ept)) {
print("Could not find your endpoint!")
}
req <- gargle::request_develop(endpoint = ept, params = params)
gar_req <- gargle::request_build(
path = req$path,
method = req$method,
params = req$params,
body = req$body,
token = token
)
gar_req
}
# test the API with just downloading the data for a form
# plus I want to see the structure to better create a future batchUpdate request
form_data_req <- my_request_generate(
"forms.forms.get",
params = list(
formId = fid
)
)
form_data_raw <- request_make(form_data_req)
gargle::response_process(form_data_raw)
This unfortunately gives me a 404 error.
Error:
! Client error: (404) Not Found
✖ Expected content type application/json, not text/html.
ℹ See
/var/folders/v9/w6xbcfv15tb191g6fft32j1h0000gn/T//Rtmp9p2m0o/gargle-unexpected-html-error-113803b594433.html
for the html error content.
ℹ Or execute
`browseURL("/var/folders/v9/w6xbcfv15tb191g6fft32j1h0000gn/T//Rtmp9p2m0o/gargle-unexpected-html-error-113803b594433.html")`
to view it in your browser.
Which just states that the url could not be found. This occurs whether I make the form private or able to be edited by anyone who has the link.
Where did I go wrong? Does the googledrive
package not have the Forms API enabled? At some point along the way something is clearly disconnected but I can't find out where.
The answer here is due to the differing structures of the Forms API versus the Drive and Sheets API. The URL for the Forms API starts with https://forms.googleapis.com
whereas the others begin with https://www.googleapis.com
. A form_data_req$url <- gsub("www.", "forms.", form_data_req$url)
did the trick!