Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# gargle (development version)

* `oauth_app_from_json` now supports JSON files from the "Web application"
client type. (#155)

# gargle 0.5.0

* [Troubleshooting gargle auth](https://gargle.r-lib.org/articles/troubleshooting.html)
Expand Down
7 changes: 6 additions & 1 deletion R/oauth-app.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ oauth_app_from_json <- function(path,
appname = NULL) {
stopifnot(is_string(path), is.null(appname) || is_string(appname))

info <- jsonlite::fromJSON(path, simplifyVector = FALSE)[["installed"]]
json <- jsonlite::fromJSON(path, simplifyVector = FALSE)
info <- json[["installed"]]
if (is.null(info)) {
# Web client credentials use this key instead
info <- json[["web"]]
}

if (!all(c("client_id", "client_secret") %in% names(info))) {
stop("Can't find 'client_id' and 'client_secret' in the JSON", call. = FALSE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"web":{"client_id":"abc.apps.googleusercontent.com","project_id":"a_project","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"ssshh-i-am-a-secret","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
10 changes: 10 additions & 0 deletions tests/testthat/test-oauth-app.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ test_that("oauth app from JSON", {
expect_identical(oa$appname, "a_project")
expect_identical(oa$secret, "ssshh-i-am-a-secret")
expect_identical(oa$key, "abc.apps.googleusercontent.com")

oa <- oauth_app_from_json(
test_path(
"fixtures", "client_secret_456.googleusercontent.com.json"
)
)
expect_s3_class(oa, "oauth_app")
expect_identical(oa$appname, "a_project")
expect_identical(oa$secret, "ssshh-i-am-a-secret")
expect_identical(oa$key, "abc.apps.googleusercontent.com")
})

test_that("JSON that is apparently not an oauth app triggers error", {
Expand Down