-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenFunctions.R
More file actions
49 lines (44 loc) · 1.74 KB
/
Copy pathtokenFunctions.R
File metadata and controls
49 lines (44 loc) · 1.74 KB
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
get_fitbit_data <- function(access_token, request.url) {
#url <- "https://api.fitbit.com/1/user/-/activities/date/today.json"
response <- GET(request.url, add_headers(Authorization = paste("Bearer", access_token)))
if (status_code(response) == 200) {
data <- content(response, as = "parsed", simplifyVector = TRUE)
return(data)
} else {
warning("Errore nella richiesta:", status_code(response))
print(content(response, as = "text"))
return(NULL)
}
}
refresh_access_token <- function(refresh_token, client_id, client_secret) {
# URL per il rinnovo del token
url <- "https://api.fitbit.com/oauth2/token"
# Codifica delle credenziali in Base64
auth_header <- paste("Basic", base64_enc(paste0(client_id, ":", client_secret)))
# Corpo della richiesta
body <- list(
grant_type = "refresh_token", # Specifica che stiamo rinnovando il token
refresh_token = refresh_token # Il token di aggiornamento corrente
)
# Richiesta POST
response <- httr::POST(
url = url,
add_headers(Authorization = auth_header), # Aggiunge l'intestazione di autorizzazione
body = body,
encode = "form" # I parametri devono essere inviati come form-data
)
# Controlla lo stato della risposta
if (httr::status_code(response) == 200) {
# Parsing della risposta in caso di successo
tokens <- httr::content(response, as = "parsed")
return(list(
access_token = tokens$access_token,
refresh_token = tokens$refresh_token
))
} else {
# Mostra l'errore se la richiesta fallisce
warning("Errore nel rinnovo del token: ", httr::status_code(response))
print(httr::content(response, as = "text")) # Mostra la risposta del server
return(NULL)
}
}