r-lib / gargle

Infrastructure for calling Google APIs from R, including auth
https://gargle.r-lib.org/
Other
113 stars 33 forks source link
authentication google package r rstats

gargle

CRAN
status Codecov test
coverage R-CMD-check

The goal of gargle is to take some of the agonizing pain out of working with Google APIs. This includes functions and classes for handling common credential types and for preparing, executing, and processing HTTP requests.

The target user of gargle is an R package author who is wrapping one of the ~250 Google APIs listed in the APIs Explorer. gargle aims to play roughly the same role as Google’s official client libraries, but for R. gargle may also be useful to useRs making direct calls to Google APIs, who are prepared to navigate the details of low-level API access.

gargle’s functionality falls into two main domains:

See the articles for holistic advice on how to use gargle.

Installation

You can install the released version of gargle from CRAN with:

install.packages("gargle")

And the development version from GitHub with:

# install.packages("pak")
pak::pak("r-lib/gargle")

Basic usage

gargle is a low-level package and does not do anything visibly exciting on its own. But here’s a bit of usage in an interactive scenario where a user confirms they want to use a specific Google identity and loads an OAuth2 token.

library(gargle)

token <- token_fetch()
#> The gargle package is requesting access to your Google account.
#> Enter '1' to start a new auth process or select a pre-authorized account.
#> 1: Send me to the browser for a new auth process.
#> 2: janedoe_personal@gmail.com
#> 3: janedoe@example.com
#> Selection: 2

token
#> ── <Token (via gargle)> ─────────────────────────────────────────────────────
#> oauth_endpoint: google
#>            app: gargle-clio
#>          email: janedoe_personal@gmail.com
#>         scopes: ...userinfo.email
#>    credentials: access_token, expires_in, refresh_token, scope, token_type, id_token

Here’s an example of using request and response helpers to make a one-off request to the Web Fonts Developer API. We show the most popular web font families served by Google Fonts.

library(gargle)

req <- request_build(
  method = "GET",
  path = "webfonts/v1/webfonts",
  params = list(
    sort = "popularity"
  ),
  key = gargle_api_key(),
  base_url = "https://www.googleapis.com"
)
resp <- request_make(req)
out <- response_process(resp)

out <- out[["items"]][1:8]
sort(vapply(out, function(x) x[["family"]], character(1)))
#> [1] "Lato"             "Material Icons"   "Montserrat"       "Noto Sans JP"    
#> [5] "Open Sans"        "Poppins"          "Roboto"           "Roboto Condensed"

Please note that the ‘gargle’ project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Privacy policy