Skip to content

Commit

Permalink
Cleanup scope handling
Browse files Browse the repository at this point in the history
Fixes #90
  • Loading branch information
jimhester committed Aug 13, 2019
1 parent 4b7b0d9 commit ad65ed3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export(gm_modify_thread)
export(gm_oauth_app)
export(gm_save_attachment)
export(gm_save_attachments)
export(gm_scopes)
export(gm_send_message)
export(gm_subject)
export(gm_text_body)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# gmailr (development)

* New `gm_scopes()` function to list the available scopes, and gmailr now
requests only the full scope by default (#90)

* All functions are now prefixed with `gm_*()` to avoid name conflicts with
other packages, the previous names have been deprecated (#95)

Expand Down
32 changes: 26 additions & 6 deletions R/gm-auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,35 @@ gargle_lookup_table <- list(
PREFIX = "gm"
)

#' @rdname rdname gm_auth
#' @export
gm_scopes <- function() {
c(labels = "https://www.googleapis.com/auth/gmail.labels",
send = "https://www.googleapis.com/auth/gmail.send",
readonly = "https://www.googleapis.com/auth/gmail.readonly",
compose = "https://www.googleapis.com/auth/gmail.compose",
insert = "https://www.googleapis.com/auth/gmail.insert",
modify = "https://www.googleapis.com/auth/gmail.modify",
metadata = "https://www.googleapis.com/auth/gmail.metadata",
settings_basic = "https://www.googleapis.com/auth/gmail.settings.basic",
settings_sharing = "https://www.googleapis.com/auth/gmail.settings.sharing",
full = "https://mail.google.com/"
)
}

#' Authorize bigrquery
#'
#' @eval gargle:::PREFIX_auth_description(gargle_lookup_table)
#' @eval gargle:::PREFIX_auth_details(gargle_lookup_table)
#' @eval gargle:::PREFIX_auth_params()
#'
#' @family auth functions
#' @param A gmail API scope to use, one of 'labels', 'send', 'readonly',
#' 'compose', 'insert', 'modify', 'metadata', 'settings_basic',
#' 'settings_sharing' or 'full' (default: 'full'). See
#' <https://developers.google.com/gmail/api/auth/scopes> for details on the
#' permissions for each scope. and `gm_scopes()` to return a vector of the
#' available scopes.
#' @export
#'
#' @examples
Expand All @@ -47,15 +69,13 @@ gargle_lookup_table <- list(
#' }
gm_auth <- function(email = gm_default_email(),
path = NULL,
scopes = c(
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/gmail.compose",
"https://mail.google.com/"
),
scopes = "full",
cache = gargle::gargle_oauth_cache(),
use_oob = gargle::gargle_oob_default(),
token = NULL) {

scopes <- gm_scopes()[match.arg(scopes, names(gm_scopes()), several.ok = TRUE)]

cred <- gargle::token_fetch(
scopes = scopes,
app = gm_oauth_app(),
Expand Down
14 changes: 9 additions & 5 deletions man/gm_auth.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 comments on commit ad65ed3

@jennybc
Copy link
Member

@jennybc jennybc commented on ad65ed3 Aug 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this gm_scopes() function and should probably think about this for other packages and/or about tooling for it with gargle. For example, we could presumably extract the relevant scopes from the Discovery Document.

@jimhester
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that is a good idea!

Please sign in to comment.