mlampros / fitbitViz

Fitbit Visualizations
https://mlampros.github.io/fitbitViz/
9 stars 5 forks source link

The request gave an error code of '403' with the following first '135' characters as error message: 'FALSE' #2

Closed gjmulder closed 3 years ago

gjmulder commented 3 years ago

OAuth working using curl (my_long_web_api_token replaced by my specific API token provided by FitBit API Debug tool):

$ curl -s -H "Authorization: Bearer my_long_web_api_token" https://api.fitbit.com/1/user/-/profile.json | python3 -mjson.tool | head -5 { "user": { "age": 49, "ambassador": false, "autoStrideEnabled": true,

USER_ID = '99xxxx'             # Specify here your 'user-id'
# Specify here your 'token'
token = "my_long_web_api_token"

heart_dat = heart_rate_time_series(user_id = USER_ID,
                                   token = token,
                                   date_start = '2021-03-09',
                                   date_end = '2021-03-16',
                                   time_start = '00:00',
                                   time_end = '23:59',
                                   detail_level = '1min',
                                   ggplot_intraday = TRUE,
                                   verbose = TRUE,
                                   show_nchar_case_error = 135)

Response:

Day: '2021-03-09' will be processed ... Error: The request gave an error code of '403' with the following first '135' characters as error message: 'list(list(errorType = "insufficient_permissions", message = "API client is not authorized by Fitbit to access the resource requested. V'The request gave an error code of '403' with the following first '135' characters as error message: 'FALSE'

mlampros commented 3 years ago

hi @gjmulder,

there are a few similar issues in the fitbit community and it seems that the 403 error is related to scope permissions. Can you have a quick look to see if it's related to yours,

Your Error comes from the sleep_single_day() function and a way to debug this Error from within R is to use the following code snippet that is used internally in the sleep_single_day() function,


  URL = glue::glue('https://api.fitbit.com/1.2/user/{user_id}/sleep/date/{date}/{date}.json')
  auth_code = paste("Bearer", token)
  query_response = httr::GET(url = URL, httr::add_headers(Authorization = auth_code))
  if (query_response$status_code != 200) {                                              # in case of an error use "httr::content()" to get more details
    content_list_obj = httr::content(query_response, "parsed")
    stop(glue::glue("The request gave an error code of '{query_response$status_code}' with the following first '{show_nchar_case_error}' characters as error message: '{substr(content_list_obj, 1, show_nchar_case_error)}'"), call. = F)
  }

  if (verbose) cat("The sleep content will be red ...\n")
  content_list_obj = httr::content(query_response, "parsed")

You have to replace the user_id, the date (for instance '2021-08-01' ) and set the show_nchar_case_error to 135 so that you can run the code snippet to receive more details.

gjmulder commented 3 years ago

Thanks for the reply. I had selected at some point "Personal" when setting up the app, but on checking it again it was set to "Server".