我应该如何使用 `gargle` 连接到 R 中的 Google Forms API?

问题描述 投票:0回答:1

我一直在 R 中创建一个使用

googledrive
googlesheets4
的闪亮应用程序。一切都很顺利,我连接了 OAuth 和一切。现在,我希望帮助我的应用程序实现更多自动化,这包括深入研究 Google Forms,因为我已经在使用基于
gargle
的代码,所以我想坚持使用它。

最终,我希望能够更新几个针对应用程序的特定用途和用户定制的表单问题,这样他们就不必自己执行此操作。但是,我没有成功进行任何 API 调用。

我首先在交互式环境(而不是在闪亮的应用程序中)测试我的代码,以确保一切正常。我首先按照 gargle

 中的说明下载 API 的发现文档
。我确实必须对他们的代码进行一些轻微的修改,但我确实成功地获得了一份发现文档。

然后我使用以下

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)
不幸的是,这给了我一个 404 错误。

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.
这只是表明无法找到该网址。无论我将表单设置为私有还是能够由知道链接的任何人进行编辑,都会发生这种情况。

我哪里做错了?

googledrive

 包是否未启用 Forms API?在这个过程中的某个时刻,某些东西明显断开了,但我找不到在哪里。

r google-forms-api gargle
1个回答
0
投票
这里的答案是由于 Forms API 与 Drive 和 Sheets API 的结构不同。 Forms API 的 URL 以

https://forms.googleapis.com

 开头,而其他 API 则以 
https://www.googleapis.com
 开头。 
form_data_req$url <- gsub("www.", "forms.", form_data_req$url)
成功了!

© www.soinside.com 2019 - 2024. All rights reserved.