简体   繁体   English

使用 googleAuthR、httr 和 jsonlite 对 Google Sheets API 的 HTTP 请求失败

[英]Failing HTTP request to Google Sheets API using googleAuthR, httr, and jsonlite

I'm attempting to request data from a google spreadsheet using the googleAuthR .我正在尝试使用googleAuthR从谷歌电子表格请求数据。 I need to use this library instead of Jenny Bryan's googlesheets because the request is part of a shiny app with multiple user authentication.我需要使用这个库而不是 Jenny Bryan 的googlesheets因为该请求是具有多用户身份验证的闪亮应用程序的一部分。 When the request range does not contain spaces (eg "Sheet1!A:B" the request succeeds. However, when the tab name contains spaces (eg "'Sheet 1'!A:B" or "\\'Sheet 1\\'!A:B" , the request fails and throws this error:当请求范围不包含空格时(例如"Sheet1!A:B"请求成功。但是,当标签名称包含空格时(例如"'Sheet 1'!A:B""\\'Sheet 1\\'!A:B" ,请求失败并抛出此错误:

Request Status Code: 400
Error : lexical error: invalid char in json text.
                                   <!DOCTYPE html> <html lang=en> 
                 (right here) ------^

Mark Edmondson's googleAuthR uses jsonlite for parsing JSON. Mark Edmondson 的googleAuthR使用jsonlite来解析 JSON。 I assume this error is coming from jsonlite , but I'm at a loss for how to fix it.我认为这个错误来自jsonlite ,但我不知道如何修复它。 Here is a minimal example to recreate the issue:这是重新创建问题的最小示例:

library(googleAuthR)

# scopes
options("googleAuthR.scopes.selected" = "https://www.googleapis.com/auth/spreadsheets.readonly")
# client id and secret
options("googleAuthR.client_id" = "XXXX")
options("googleAuthR.client_secret" = "XXXX")

# request
get_data <- function(spreadsheetId, range) {

    l <- googleAuthR::gar_api_generator(
        baseURI = "https://sheets.googleapis.com/v4/",
        http_header = 'GET',
        path_args = list(spreadsheets = spreadsheetId,
                         values = range),
        pars_args = list(majorDimension = 'ROWS',
                         valueRenderOption = 'UNFORMATTED_VALUE'),
        data_parse_function = function(x) x)

    req <- l()

    req
}

# authenticate
gar_auth(new_user = TRUE)

# input
spreadsheet_id <- "XXXX"
range <- "'Sheet 1'!A:B"

# get data
df <- get_data(spreadsheet_id, range)

How should I format range variable for the request to work?我应该如何格式化range变量以使请求工作? Thanks in advance for the help.在此先感谢您的帮助。

Use URLencode() to percent-encode spaces.使用URLencode()对空格进行百分比编码。

Details:细节:

Using options(googleAuthR.verbose = 1) shows that the GET request was of the form:使用options(googleAuthR.verbose = 1)表明 GET 请求的形式为:

GET /v4/spreadsheets/.../values/'Sheet 1'!A:B?majorDimension=ROWS&valueRenderOption=UNFORMATTED_VALUE HTTP/1.1

I had assumed the space would be encoded, but I guess not.我原以为空间会被编码,但我猜不是。 In this github issue from August 2016, Mark states URLencode() was going to be the default for later versions of googleAuthR .在 2016 年 8 月的这个github 问题中,Mark 表示URLencode()将成为googleAuthR更高版本的默认值。 Not sure if that will still happen, but it's an easy fix in the meantime.不确定这是否仍然会发生,但在此期间很容易解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM