You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
convert vignette rmd code chunks to plain R chunks to prevent eval (#303)
* convert vignette rmd code chunks to plain R chunks to prevent eval, closes#301
* avoid butchering backticks
* Let's focus only on the single, troublesome vignette
* Style the way Air would + typo fix
* Revert changes to README as well
---------
Co-authored-by: Jenny Bryan <jenny.f.bryan@gmail.com>
Copy file name to clipboardExpand all lines: NEWS.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# gargle (development version)
2
2
3
+
* In vignettes, convert all `{r eval=FALSE}` chunks to plain `r` chunks to prevent any chance of vignette code evaluation while maintaining R syntax highlighting (#301, @tanho63)
4
+
3
5
# gargle 1.6.0
4
6
5
7
* When retrying a request, the messaging reveals more detail about the failed
Copy file name to clipboardExpand all lines: vignettes/non-interactive-auth.Rmd
+38-41Lines changed: 38 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -7,15 +7,6 @@ vignette: >
7
7
%\VignetteEncoding{UTF-8}
8
8
---
9
9
10
-
```{r, include = FALSE}
11
-
knitr::opts_chunk$set(
12
-
collapse = TRUE,
13
-
comment = "#>",
14
-
eval = FALSE,
15
-
purl = FALSE
16
-
)
17
-
```
18
-
19
10
Here we describe how to do auth with a package that uses gargle, without requiring any user interaction.
20
11
This comes up in a wide array of contexts, ranging from simple rendering of a local R Markdown document to deploying a data product on a remote server.
21
12
@@ -56,7 +47,7 @@ When you embed tokens in the project and deploy, remember, that, by default, the
56
47
TL;DR is that you need to successfully authenticate *once* in an interactive session and then, in your code, give gargle permission to use a token it finds in the cache.
57
48
These sorts of commands achieve that:
58
49
59
-
```{r}
50
+
```r
60
51
# Approach #1: use an option.
61
52
# Either specify the user:
62
53
options(gargle_oauth_email="jenny@example.com")
@@ -102,13 +93,17 @@ GCE allows applications to get an OAuth access token from its metadata server an
102
93
This token request can be made for specific scopes and, in general, most wrapper packages will indeed be asking for specific scopes relevant to the API they access.
103
94
Consider the signature of `googledrive::drive_auth()`:
The googledrive package asks for a token with `"drive"` scope, by default.
@@ -124,7 +119,7 @@ Be aware that you might also need to explicitly grant the service account an app
124
119
Finally, if you want to opt-out of using the default service account and, instead, auth as a normal user, even though you are on GCE, that is also possible.
125
120
One way to achieve that is to remove `credentials_gce()` from the set of auth functions tried by `gargle::token_fetch()` by executing this command before any explicit or implicit auth happens:
126
121
127
-
```{r}
122
+
```r
128
123
# removes `credentials_gce()` from gargle's registry
129
124
gargle::cred_funs_add(credentials_gce=NULL)
130
125
```
@@ -178,9 +173,11 @@ Documentation around `GKEStartPodOperator()` within Cloud Composer can be found
178
173
179
174
Here is example code that you might execute in your Docker container:
180
175
181
-
```{r}
176
+
```r
182
177
options(gargle.gce.use_ip=TRUE)
183
-
t <- gargle::credentials_gce("my-service-key@my-project.iam.gserviceaccount.com")
It is also possible to get a token with an explicit call to, e.g.,
254
251
`credentials_service_account()` and then pass that token to the auth function:
255
252
256
-
```{r}
253
+
```r
257
254
t<-gargle::credentials_service_account(
258
255
path="/path/to/your/service-account-token.json",
259
256
scopes=...,
260
257
subject="user@example.com"
261
258
)
262
-
googledrive::dive_auth(token = t)
259
+
googledrive::drive_auth(token=t)
263
260
```
264
261
265
262
If delegation of domain-wide authority is impossible or unappealing, you must use an OAuth user token, as described below.
@@ -277,7 +274,7 @@ Your token should just get discovered upon first need.
277
274
278
275
For troubleshooting purposes, you can set a gargle option to see verbose output about the execution of `gargle::token_fetch()`:
279
276
280
-
```{r}
277
+
```r
281
278
options(gargle_verbosity="debug")
282
279
```
283
280
@@ -287,7 +284,7 @@ withr-style convenience helpers also exist: `with_gargle_verbosity()` and `local
287
284
288
285
If you somehow have the OAuth token you want to use as an R object, you can provide it directly to the `token` argument of the main auth function. Example using googledrive:
289
286
290
-
```{r}
287
+
```r
291
288
library(googledrive)
292
289
293
290
my_oauth_token<-# some process that results in the token you want to use
@@ -298,7 +295,7 @@ gargle caches each OAuth user token it obtains to an `.rds` file, by default.
298
295
If you know the filepath to the token you want to use, you could use `readRDS()` to read it and provide as the `token` argument to the wrapper's auth function.
@@ -327,7 +324,7 @@ There are many ways to do this. We'll work several examples using that convey th
327
324
328
325
**Step 1**: Get that first token. You must run your code at least once, interactively, do the auth dance, and allow gargle to store the token in its cache.
329
326
330
-
```{r}
327
+
```r
331
328
library(googledrive)
332
329
333
330
# do anything that triggers auth
@@ -353,37 +350,37 @@ You have two choices to make:
353
350
354
351
This sets an option that allows gargle to use cached tokens whenever there's a unique match:
355
352
356
-
```{r}
353
+
```r
357
354
options(gargle_oauth_email=TRUE)
358
355
```
359
356
360
357
This sets an option to use tokens associated with a specific email address:
361
358
362
-
```{r}
359
+
```r
363
360
options(gargle_oauth_email="jenny@example.com")
364
361
```
365
362
366
363
This sets an option to use tokens associated with an email address with a specific domain:
367
364
368
-
```{r}
365
+
```r
369
366
options(gargle_oauth_email="*@example.com")
370
367
```
371
368
372
369
This gets a token *right now* and allows the use of a matching token, using googledrive as an example:
373
370
374
-
```{r}
371
+
```r
375
372
drive_auth(email=TRUE)
376
373
```
377
374
378
375
This gets a token *right now*, for the user with a specific email address:
379
376
380
-
```{r}
377
+
```r
381
378
drive_auth(email="jenny@example.com")
382
379
```
383
380
384
381
This gets a token *right now*, first checking the cache for a token associated with a specific domain:
385
382
386
-
```{r}
383
+
```r
387
384
drive_auth(email="*@example.com")
388
385
```
389
386
@@ -394,7 +391,7 @@ This is like the previous example, but with an added twist: we use a project-lev
394
391
**Step 1**: Obtain the token intended for non-interactive use and make sure it's cached in a (hidden) directory of the current project.
395
392
Using googledrive as an example:
396
393
397
-
```{r}
394
+
```r
398
395
library(googledrive)
399
396
400
397
# designate project-specific cache
@@ -414,7 +411,7 @@ Do this setup once per project.
414
411
415
412
Another way to accomplish the same setup is to specify the desired cache location directly in the call to the auth function:
416
413
417
-
```{r}
414
+
```r
418
415
library(googledrive)
419
416
420
417
# trigger auth on purpose --> store a token in the specified cache
**Step 2**: In all downstream use, announce the location of the cache and pre-authorize the use of a suitable token discovered there. Continuing the googledrive example:
425
422
426
-
```{r}
423
+
```r
427
424
library(googledrive)
428
425
429
426
options(
@@ -442,7 +439,7 @@ Depending on the context, it might be suitable to accomplish this in a startup f
442
439
Here's a variation where we say which token to use by explicitly specifying the associated email.
443
440
This is handy if there's a reason to have more than one token in the cache.
444
441
445
-
```{r}
442
+
```r
446
443
library(googledrive)
447
444
448
445
options(
@@ -456,7 +453,7 @@ drive_find(n_max = 5)
456
453
457
454
Here's another variation where we specify the necessary info directly in an auth call, instead of in options:
458
455
459
-
```{r}
456
+
```r
460
457
library(googledrive)
461
458
462
459
drive_auth(cache=".secrets", email=TRUE)
@@ -467,7 +464,7 @@ drive_find(n_max = 5)
467
464
468
465
Here's one last variation that's applicable when the local cache could contain multiple tokens:
0 commit comments