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