R/auth.R

Defines functions scr_auth

Documented in scr_auth

#' Do OAuth2 authentication
#' 
#' @param token Where you want to save the auth file, or an existing token or file location of a token to authenticate with
#' @param email An email you have authenticated with previously
#' 
#' @details
#' 
#' Run this function first time to authenticate with Google in your browser.  
#' 
#' After initial authentication, a \code{sc.oauth} will be saved to your working directory, where your authentication details are kept.  Keep this file safe.
#' 
#' If you want to reauthenticate, delete this file from your directory or run \code{scr_auth(new_user = TRUE)}
#' 
#' @section Multiple accounts:
#' 
#' You can authenticate with a new auth file for each account. Supply argument \code{token} with the name of the cache file you want to use e.g. \code{scr_auth(token = "one.httr-oauth")} for one account, 
#'   \code{scr_auth(token = "another.httr-oauth")} for a different account.
#'
#' 
#' @section Auto-authentication:
#' 
#' You can choose to auto-authenticate by moving your \code{sc.httr-oauth} or by 
#'   creating a Google OAuth service account JSON file.
#' 
#' Specify an environment variable in R via a \code{.Renviron} file or using \link{Sys.setenv} which point to the file location of your chosen authentication file.  See \link{Startup}
#' 
#' Once you have set the environment variable \code{SC_AUTH_FILE} to a valid file location,
#'   the function will look there for authentication details upon loading the library meaning 
#'   you will not need to call \code{scr_auth()} yourself as you would normally.
#' 
#' An example \code{.Renviron} file is below:
#' 
#' \code{SC_AUTH_FILE = "/Users/bob/auth/sc.oauth"}
#'
#' \code{SC_AUTH_FILE} can be either a auth file for a token generated by \link[googleAuthR]{gar_auth} or
#'   service account JSON ending with file extension \code{.json}
#'
#' @section Your own Google Project:
#' 
#' Be default the Google Project used is shared by all users, this is usually sufficient, but  
#'   you could choose to create your own Google Project and turn on the Webmaster APIs.
#' 
#' You can then download your own client JSON, and set by placing in the \code{GAR_CLIENT_JSON} environment argument.  See \link[googleAuthR]{gar_set_client} for details.
#' 
#' @section Service accounts:
#'   
#' If you use the service account JSON, you will need to add the service account email 
#'   to your Search Console users to see data e.g. \code{xxxx@yyyyyy.iam.gserviceaccount.com}
#'   
#' @seealso \code{\link[googleAuthR]{gar_auth}}
#' 
#' @export
#' @import googleAuthR
scr_auth <- function(token=NULL, email = NULL){
  
  gar_set_client(system.file("clients","native.json", package = "searchConsoleR"),
                              scopes = "https://www.googleapis.com/auth/webmasters")
  
  gar_auth(token=token, email=email)
  
}

Try the searchConsoleR package in your browser

Any scripts or data that you put into this service are public.

searchConsoleR documentation built on Sept. 6, 2019, 5:02 p.m.