Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Empty auth_token using httr (R)

Hello,

I'm able to get an access token using the fitbitr package https://github.com/teramonagi/fitbitr

 

However, i'm trying to login using a code, but the auth_token i get is empty

 

create_endpoint <- function()
{
  request <- "https://api.fitbit.com/oauth2/token"
  authorize <- "https://www.fitbit.com/oauth2/authorize"
  access <- "https://api.fitbit.com/oauth2/token"
  httr::oauth_endpoint(request, authorize, access)
}

fitbit <- create_endpoint()

# Read key and secret
source("~/.fitbitr")

key <- api_keys$key
secret <- api_keys$secret

#Create App
fitbit_app <- httr::oauth_app("fitbit", key = key, secret = secret, redirect_uri = "http://localhost:1410/")

# Scope
scope <- c("sleep", "activity", "heartrate", "location", "nutrition", "profile", "settings", "social", "weight")

# Get the code
url <- httr::BROWSE(httr::oauth2.0_authorize_url(fitbit, fitbit_app, scope = "sleep", redirect_uri = "http://localhost:1410/"))

accesstoken <- httr::oauth2.0_access_token(fitbit,
                                           fitbit_app,
                                           code = "57ebc797058c1394d5c0dba8b989d24457313c79",  # Code i get with oauth2.0_authorize_url
                                           user_params = fitbit
                                           )

header <- httr::add_headers(Authorization = paste0("Basic ",
                                                 RCurl::base64Encode(charToRaw(paste0(api_keys$key, ":", api_keys$secret)))))
content_type <- httr::content_type("application/x-www-form-urlencoded")

token <- httr::oauth2.0_token(
  endpoint = fitbit,
  app = fitbit_app,
  scope = scope,
  config_init = c(header, content_type),
  use_basic_auth = TRUE,
  query_authorize_extra = list(prompt = "login"),
  type = "code",
  credentials = accesstoken,
  cache = FALSE
)

# I Get
# token$credentials$node
# <pointer: 0x55c4b0716890>


# Another try
req <- httr::POST(fitbit$access, encode = "form",
                  body = list(
                    client_id = fitbit_app$key,
                    redirect_uri = fitbit_app$redirect_uri,
                    grant_type = "authorization_code",
                    code = "57ebc797058c1394d5c0dba8b989d24457313c79"),
                  httr::authenticate(fitbit_app$key, fitbit_app$secret))


# I get
# req$request$auth_token
# NULL
Best Answer
0 Votes
1 REPLY 1

Hi @cro87,

 

The authorization code is generated by Fitbit and will be returned to your application when the user consents to share their data.  The authorization code can be found as an appended parameter in the callback URL.  Your application should not have it hardcoded.

 

My recommendation is to try the OAuth 2.0 Tutorial, https://dev.fitbit.com/build/reference/web-api/troubleshooting-guide/debugging-tools/#oauth-2-0-tuto....  It'll walk you through the steps to generate the access token using the returned authorization code.

 

If you need further assistance with this, please let me know.

 

Gordon

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes