#Using Dropbox API in R via {httr}
Connect to DropBox API via OAuth2.0 in R and manipulate folders and files using {httr}
package.
The following uses Dropbox API v2
# Provide a .csv file containing your apps credentials with the following header
# name, key, secret (
app_config_file <- 'dropbox_credentials.csv' # my file
app_config <- read.csv(app_config_file, stringsAsFactors = FALSE)
# load {httr} package
library("httr")
db_app <- oauth_app(appname = app_config$name, key = app_config$key, secret = app_config$secret)
# Set request = NULL for OAuth2
# authorize is the url to send to send client for authorization
# access is the url used to exchange token
db_endpoint <- oauth_endpoint(request = NULL,
authorize = 'https://www.dropbox.com/1/oauth2/authorize',
access = 'https://api.dropboxapi.com/1/oauth2/token')
# set cache = TRUE to cache token
db_token <- oauth2.0_token(endpoint = db_endpoint, app = db_app, cache = FALSE)
# will open your browser for authorization
# @INFO API urls are (api.dropboxapi.com, content.dropboxapi.com, notify.dropboxapi.com)
# Example API call to get the current user info
user_info_req2 <- POST(url = 'https://api.dropboxapi.com/2/users/get_current_account', config = config(token = db_token))
# Check request status and errors
stop_for_status(user_info_req2)
# List content
content(user_info_req2)
# the API call
list_folder_req <- POST(url = "https://api.dropboxapi.com/2/files/list_folder", config = config(token <- db_token), content_type_json(), body = "{\"path\":\"\" }")
# Check status and errors
stop_for_status(list_folder_req)
# List content
content(list_folder_req)
## An example with the first entry of my app folder
folder_content = content(list_folder_req)
first_file <- folder_content$entries[[1]]$path_lower
# create the json data for the body (respect double quotes) or use {jsonlite} package
json_data <- paste('{"path":"', first_file, '"}', sep = "")
# API Call
get_metadata_req <- POST(url = "https://api.dropboxapi.com/2/files/get_metadata", config = config(token = db_token), content_type_json(), body = json_data)
# Check status and errors
stop_for_status(get_metadata_req)
# List content
content(get_metadata_req)