forked from Tatvic/RGoogleAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuth.R
67 lines (65 loc) · 2.38 KB
/
Auth.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#' Authorize the RGoogleAnalytics package to the user's Google Analytics
#' Account using OAuth2.0
#'
#' @description
#' This function expects a Client ID and Client Secret. In order to obtain
#' these, you will have to register an application with the Google Analytics
#' API. This can be done as follows
#'
#' - Go to \url{https://console.developers.google.com}
#'
#' - Create a New Project and enable the Google Analytics API
#'
#' - On the Credentials screen, create a new Client ID for Application Type
#' "Installed Application".
#'
#' - Copy the Client ID and Client Secret to your R Script as shown in the
#' Examples section below
#'
#'
#'
#' @param client.id Equivalent to a user name
#' @param client.secret Equivalent to a password
#'
#'
#' @details
#' When evaluated for the first time this function asks for User Consent
#' for the Google Analytics Account and creates a OAuth Token Object.
#' The token object can be saved locally to a file on the user's system.
#' In subsequent runs, User Consent is not required unless you are querying a
#' Google Analytics profile associated with a different email account.
#' This function uses \code{\link[httr]{oauth2.0_token}} under the hood to
#' create the OAuth Tokens. The Access Token has a 60 minute lifetime after
#' which it expires and a new token has to be obtained. This can be done using
#' the \code{\link{ValidateToken}} method
#'
#' @export
#'
#' @return google.token A Token object containing all the data required for
#' OAuth access. See \code{\link[httr]{Token2.0}} for additional information
#' on the Token object
#'
#' @examples \dontrun{
#' # Generate the oauth_token object
#' oauth_token <- Auth(client.id = "150487456763-XXXXXXXXXXXXXXX.apps.googleusercontent.com",
#' client.secret = "TUXXXXXXXXXXXX_TknUI")
#'
#' # Save the token object for future sessions
#' save(oauth_token, file="oauth_token")
#'
#' # Load the token object
#' load("oauth_token")
#' }
#'
#' @importFrom httr oauth_app
#' @importFrom httr oauth2.0_token
#' @importFrom httr oauth_endpoints
#'
Auth <- function(client.id,client.secret) {
# Whether to allow user to save client secret to disk?
myapp <- oauth_app("google", client.id,
client.secret)
google.token <- oauth2.0_token(oauth_endpoints("google"), myapp,
scope = "https://www.googleapis.com/auth/analytics.readonly")
return(google.token)
}