Skip to content

Instantly share code, notes, and snippets.

@lietwin
Created October 21, 2015 13:48
Show Gist options
  • Save lietwin/b737664f66f4eb38c01f to your computer and use it in GitHub Desktop.
Save lietwin/b737664f66f4eb38c01f to your computer and use it in GitHub Desktop.
Dropbox API OAuth2 authentication and API calls in R via the package {httr}

#Using Dropbox API in R via {httr}

Description

Connect to DropBox API via OAuth2.0 in R and manipulate folders and files using {httr} package. The following uses Dropbox API v2

Connecting to Dropbox API

Get the Dropbox app settings (name, key etc.)

# 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")

Create an OAuth application

db_app <- oauth_app(appname = app_config$name, key = app_config$key, secret = app_config$secret)

Describe the OAuth endpoint

# 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')

Generate an OAuth 2.0 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

Where to go from here? use the Dropbox APIv2

Dropbox API hostnames

# @INFO API urls are (api.dropboxapi.com, content.dropboxapi.com, notify.dropboxapi.com)

Get current user info

# 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)

Get information about files and folders

List app folder

# 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)

Get app folder or file metadata

## 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment