GoogleOAuth.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr) | |
library(yaml) | |
library(RGoogleAnalytics) | |
library(rjson) | |
library(RCurl) | |
library(bitops) | |
### | |
# | |
### | |
config_file = "config.yml" | |
config = yaml.load_file(config_file) | |
client_secret = "JwJmwe_L6iHZgyZ6daWO2AF9" | |
client_id = "907346070567.apps.googleusercontent.com" | |
redirect_uri = "urn:ietf:wg:oauth:2.0:oob" | |
isValid <- function() { | |
access_token = config$access_token | |
refresh_token = config$refresh_token | |
expired_date = config$expired_date | |
if (is.null(refresh_token)) { | |
return(0) | |
} | |
#now = date() | |
now = as.numeric(Sys.time()) | |
print("expired_date:") | |
print(expired_date) | |
print("now:") | |
print(now) | |
print("now - expired_in:") | |
print(now - expired_date) | |
#print(difftime(now, expired_date)) | |
if(now - expired_date > 3000 ){ | |
return(0) | |
} | |
return (1) | |
} | |
update_tokens <- function(access_token, refresh_token, date) { | |
print("update_tokens...") | |
a <- paste("access_token", access_token, sep=": ") | |
cat(a, file="config.yml", sep="\n",append = FALSE) | |
r <- paste("refresh_token", refresh_token, sep=": ") | |
cat( r , file="config.yml", sep="\n",append = TRUE) | |
cat("expired_in: 3600", file="config.yml", sep="\n",append = TRUE) | |
e <- paste("expired_date", date, sep=": ") | |
cat(e, file="config.yml", sep="\n",append = TRUE) | |
print("update_tokens done!") | |
} | |
get_request_token <- function() { | |
browseURL(paste("https://accounts.google.com/o/oauth2/auth?scope=", | |
"https://www.googleapis.com/auth/analytics.readonly&", | |
"response_type=code&access_type=offline&redirect_uri=urn:ietf:wg:oauth:2.0:oob&", | |
"aprove_prompt=force&client_id=907346070567.apps.googleusercontent.com&hl=en&from_login=1&as=", | |
"7886e0e26859b9a5&pli=1&authuser=0", | |
sep="")) | |
request_token <- readline(as.character(cat("\n\nPaste the request token here",":=>"))) | |
return(request_token) | |
} | |
get_access_token <- function(request_token) { | |
grant_type = "authorization_code" | |
content = postForm(access_token_url, | |
"redirect_uri" = redirect_uri, | |
"client_secret" = client_secret, | |
"client_id" = client_id, | |
"grant_type" = grant_type, | |
"code" = request_token) | |
y <- fromJSON(content) | |
print("y$access_token :") | |
print(y$access_token) | |
return(y$access_token) | |
} | |
get_access_token_and_refresh_token <- function(request_token) { | |
grant_type = "authorization_code" | |
content = postForm(access_token_url, | |
"redirect_uri" = redirect_uri, | |
"client_secret" = client_secret, | |
"client_id" = client_id, | |
"grant_type" = grant_type, | |
"code" = request_token) | |
return (content) | |
} | |
GetAccessToken <- function() { | |
access_token = config$access_token | |
refresh_token = config$refresh_token | |
if(isValid() == 1) { | |
print("isValid") | |
} else { | |
print("!isValid") | |
request_token <- get_request_token() | |
print("request_token") | |
print(request_token) | |
content = get_access_token_and_refresh_token(request_token) | |
y <- fromJSON(content) | |
access_token = y$access_token | |
if (is.null(refresh_token)) { | |
refresh_token = y$refresh_token | |
} | |
update_tokens(access_token, refresh_token,as.numeric(Sys.time())) | |
} | |
return (access_token) | |
} | |
refresh <- function() { | |
refresh_token = config$refresh_token | |
grant_type = "refresh_token" | |
r <- POST(access_token_url, | |
body = list("redirect_uri" = redirect_uri, | |
"client_secret" = client_secret, | |
"client_id" = client_id, | |
"grant_type" = grant_type | |
)) | |
y = content(r, as="parsed") | |
print (y) | |
return(y$access_token) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment