From 878cfc3bb4719736c1aad1ee8ce835612515345f Mon Sep 17 00:00:00 2001 From: samanthacsik Date: Thu, 3 Oct 2024 00:31:33 -0400 Subject: [PATCH] Add in token refresh --- R/keys-from-env.R | 4 +++- R/scrape_strava.R | 13 ++++++----- R/strava_authentication.R | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 R/strava_authentication.R diff --git a/R/keys-from-env.R b/R/keys-from-env.R index 3b9f953..e216128 100644 --- a/R/keys-from-env.R +++ b/R/keys-from-env.R @@ -1,4 +1,6 @@ - app_name <- "SamHeatmaps" app_client_id <- Sys.getenv("STRAVA_KEY") app_secret <- Sys.getenv("STRAVA_SECRET") +strava_secret_token <- Sys.getenv("STRAVA_ACCESS_TOKEN") +strava_s3_bucket <- "sams-strava-dashboard-data" +refresh_token_filename <- "strava_refresh_token.txt" diff --git a/R/scrape_strava.R b/R/scrape_strava.R index 693ddc2..eb38cba 100644 --- a/R/scrape_strava.R +++ b/R/scrape_strava.R @@ -16,18 +16,21 @@ library(aws.s3) #.......Strava app name, client ID, secret, and athlete id....... source("R/keys-from-env.R") +#.......Strava refresh token dance....... +source("R/strava_authentication.R") +my_token <- retrieve_strava_token() + #......................create strava token....................... -my_token <- httr::config(token = strava_oauth(app_name, app_client_id, app_secret, - app_scope = "activity:read_all", - cache = TRUE)) +# my_token <- httr::config(token = strava_oauth(app_name, app_client_id, app_secret, +# app_scope = "activity:read_all", +# cache = TRUE)) -print(my_token) ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## scrape data ---- ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #....................scrape strava activities.................... -my_actvities <- rStrava::get_activity_list(stoken = my_token) +my_actvities <- rStrava::get_activity_list(my_token) ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## wrangle / clean data ---- diff --git a/R/strava_authentication.R b/R/strava_authentication.R new file mode 100644 index 0000000..1c0b54f --- /dev/null +++ b/R/strava_authentication.R @@ -0,0 +1,45 @@ +library(rStrava) +library(aws.s3) + +source("R/keys-from-env.R") + + +refresh_strava_token <- function(refresh_token) { + res <- httr::POST( + url = "https://www.strava.com/oauth/token", + body = list( + client_id = app_client_id, + client_secret = app_secret, + grant_type = "refresh_token", + refresh_token = refresh_token + ) + ) + + new_token <- httr::content(res) + return(new_token) +} + +retrieve_strava_token <- function() { + strava_refresh_token <- rawToChar(get_object(refresh_token_filename, bucket=strava_s3_bucket)) + + strava_app <- httr::oauth_app("strava", key = app_client_id, secret = app_secret) + + strava_end <-httr::oauth_endpoint( + request = "https://www.strava.com/oauth/authorize?", + authorize = "https://www.strava.com/oauth/authorize", + access = "https://www.strava.com/oauth/token") + + new_token <- refresh_strava_token(strava_refresh_token) + + put_object(file = charToRaw(new_token$refresh_token), object=refresh_token_filename, bucket=strava_s3_bucket) + + token <- httr::oauth2.0_token( + endpoint = strava_end, + app = strava_app, + credentials = new_token, + scope = "activity:read_all", + cache = TRUE + ) + # Create a token object + httr::config(token = token) +}