get_bitly_client_credentials <- function (convert_to_base64 = TRUE) {
## THIS IS NOT USED.
##  I had copy+pasted this from the spotify API scrape
    ret <- sprintf("%s:%s", get_bitly_client_id(), get_bitly_client_secret())
    if (isTRUE(convert_to_base64))
        return(RCurl::base64(ret))
    return(ret)
}

get_bitly_base_url <- function() {
    getOption("bitly_base_url")
}

get_bitly_client_id <- function() {
    getOption("bitly_client_id")
}

get_bitly_client_secret <- function() {
    getOption("bitly_client_secret")
}

get_bitly_uid <- function() {
    getOption("bitly_uid")
}

get_bitly_pw <- function() {
    getOption("bitly_pw")
}


is_expired_bitly_token <- function(token) {
## it is unclear if/when bitly tokens expire. For now, we will go with never, until we figure out otherwise
  FALSE
}

get_bitly_token <- function(verbose=TRUE, refresh=FALSE, client_id=getOption("bitly_client_id"), envir=globalenv()) {

  if (!refresh && exists("bitly_token", envir=envir)) {
    bitly_token <- get("bitly_token", envir=envir)
    expired <- is_expired_bitly_token(bitly_token)
    if (expired) 
      verboseMsg(verbose, "'bitly_token' exists in the environment but it is expired. Will refresh")
    else 
      return(bitly_token)
  }

  verboseMsg(verbose, "Getting a new bitly_token")

  url_for_token <- "https://api-ssl.bitly.com/oauth/access_token"
  cmd <- sprintf('curl -u "%s:%s" -X POST "%s"', get_bitly_uid(), get_bitly_pw(), url_for_token)

  time_start <- now()
  raw <- system(cmd, intern=TRUE)
  ## no need for json parsing, expiration, etc
  ##   ret <- jsonlite::fromJSON(raw)
  ##   ret[["time_start"]] <- time_start
  ##   ret[["time_end"]]  <- time_start + ret$expires_in

  ret <- raw  
  classAppend_(ret, "bitly_token")

  ## assign into envir
  assign("bitly_token", value=ret, envir=envir)
  
  return(ret)
}