Skip to content

Commit

Permalink
Add in token refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
samanthacsik committed Oct 3, 2024
1 parent ee11ab8 commit 878cfc3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
4 changes: 3 additions & 1 deletion R/keys-from-env.R
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 8 additions & 5 deletions R/scrape_strava.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----
Expand Down
45 changes: 45 additions & 0 deletions R/strava_authentication.R
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 878cfc3

Please sign in to comment.