Skip to content

Instantly share code, notes, and snippets.

@aelkiss
Created April 21, 2021 16:50
Show Gist options
  • Save aelkiss/fe317f4f26edd7b37a7c53d3e3a2aff5 to your computer and use it in GitHub Desktop.
Save aelkiss/fe317f4f26edd7b37a7c53d3e3a2aff5 to your computer and use it in GitHub Desktop.
Create and share a folder with Dropbox HTTP API
#!ruby
require 'json'
require 'faker'
require 'faraday'
# Example of using the Dropbox HTTP API to create and share a folder.
#
# Usage:
#
# - Install faraday with gem install or via bundler
# - Generate a Dropbox access token: https://dropbox.tech/developers/generate-an-access-token-for-your-own-account
# - Run:
#
# ruby dropbox_add_collab.rb /path/to/folder [email protected] [email protected]
class DropboxClient
DROPBOX_ENDPOINT = 'https://api.dropboxapi.com/2'
def initialize(access_token)
@access_token = access_token
end
def post(endpoint,data)
JSON.parse(Faraday.post(DROPBOX_ENDPOINT + endpoint,data.to_json,
{"Content-Type" => "application/json",
"Authorization" => "Bearer #{@access_token}"}).body)
end
end
access_token = ENV[DROPBOX_ACCESS_TOKEN]
client = DropboxClient.new(access_token)
newpath = ARGV.shift
puts "Making new folder #{newpath}"
resp = client.post('/files/create_folder_v2', {
"path" => newpath,
"autorename" => false})
pp resp
puts "Converting folder #{newpath} to shared folder"
resp = client.post('/sharing/share_folder', {
"path" => newpath,
"acl_update_policy" => "editors",
"force_async" => false,
"member_policy" => "anyone",
"shared_link_policy" => "members",
"access_inheritance" => "inherit"
})
pp resp
shared_folder_id = resp['shared_folder_id']
shared_users = ARGV
puts "Sharing folder with #{shared_users}"
resp = client.post('/sharing/add_folder_member',
{"shared_folder_id"=>shared_folder_id,
"members"=> shared_users.map do |email|
{
"member"=> {
".tag"=>"email",
"email"=>email
},
"access_level"=>"editor"
}
end
})
pp resp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment