Skip to content

Commit

Permalink
Rework gmail_auth to take secret file parameter
Browse files Browse the repository at this point in the history
Also reoxygenate
  • Loading branch information
jimhester committed Dec 31, 2015
1 parent df640db commit f88b8d4
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 14 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -83,6 +83,7 @@ export(untrash_message)
export(untrash_thread)
export(update_label)
export(update_label_patch)
export(use_secret_file)
import(base64enc)
import(httr)
importFrom(magrittr,"%>%")
45 changes: 34 additions & 11 deletions R/gmailr.R
Expand Up @@ -43,25 +43,36 @@ clear_token <- function() {
}

#' Setup oauth authentication for your gmail
#' @param secret_file the secret json file downloaded from \url{https://cloud.google.com/console#/project}
#'
#' @param scope the authentication scope to use
#' @param id the client_id to use for authentication
#' @param secret the client secret to use for authentication
#' @param secret_file the secret json file downloaded from \url{https://cloud.google.com/console#/project}
#' @seealso use_secret_file to set the default id and secret to a different
#' value than the default.
#' @export
#' @examples
#' \dontrun{
#' gmail_auth("file", "compose")
#' gmail_auth("compose")
#' }
gmail_auth <- function(secret_file=NULL,
gmail_auth <- function(scope=c("read_only", "modify", "compose", "full"),
id = the$id,
secret = the$secret,
scope=c("read_only", "modify", "compose", "full")) {

if(is.null(secret_file)){
myapp <- oauth_app("google", id, secret)
} else {
info <- jsonlite::fromJSON(readChar(secret_file, nchars=1e5))
myapp <- oauth_app("google", info$installed$client_id, info$installed$client_secret)
secret_file = NULL) {

if(!is.null(secret_file)){
if (!(missing(id) && missing(secret))) {
stop("You should set either ", sQuote("secret_file"), " or ",
sQuote("id"), " and ", sQuote("secret"), ", not both",
call. = FALSE)
}
use_secret_file(secret_file)

# Use new ID and secret
id <- the$id
secret <- the$secret
}

myapp <- oauth_app("google", id, secret)

scope <- switch(match.arg(scope),
read_only = "https://www.googleapis.com/auth/gmail.readonly",
Expand All @@ -73,6 +84,18 @@ gmail_auth <- function(secret_file=NULL,
the$token <- oauth2.0_token(oauth_endpoints("google"), myapp, scope = scope)
}

#' Use information from a secret file
#'
#' This function sets the default secret and client_id to those in the secret
#' file
#' @param filename the filename of the file
#' @export
use_secret_file <- function(filename) {
info <- jsonlite::fromJSON(readChar(secret_file, nchars=1e5))
the$secret <- info$installed$client_secret
the$client_id <- info$installed$client_id
}

#' Get the body text of a message or draft
#' @param x the object from which to retrieve the body
#' @param ... other parameters passed to methods
Expand Down
12 changes: 12 additions & 0 deletions man/clear_token.Rd

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

14 changes: 11 additions & 3 deletions man/gmail_auth.Rd

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

12 changes: 12 additions & 0 deletions man/last_response.Rd

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

16 changes: 16 additions & 0 deletions man/use_secret_file.Rd

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

0 comments on commit f88b8d4

Please sign in to comment.