Skip to content

Instantly share code, notes, and snippets.

@aelkiss
Last active April 21, 2021 17:11
Show Gist options
  • Save aelkiss/5a7119a36cc6570041a9493903d2020c to your computer and use it in GitHub Desktop.
Save aelkiss/5a7119a36cc6570041a9493903d2020c to your computer and use it in GitHub Desktop.
List external collaborators using the box API
require 'json'
require 'boxr'
# box_list_collabs.rb: List external collaborators for a Box folder and all its subfolders on box.
#
# Output format: tab separated values: parent folder, item name, collaborator name, collaborator login, collaborator role
#
# Usage:
#
# - Install the boxr gem (https://github.com/cburnette/boxr) either with 'gem
# install' or bundler
#
# - Generate a developer key with Box: https://github.com/cburnette/boxr#usage
# and set the BOX_DEVELOPER_TOKEN environment variable with it
#
# - Run as: ruby box_list_collabs.rb folder
#
# Additional reference: https://developer.box.com/reference/get-folders-id-collaborations/
client = Boxr::Client.new(ENV['BOX_DEVELOPER_TOKEN'])
def list_collabs(path,folder,client)
client.folder_items(folder).select { |item| item['type'] == 'folder' }.each do |item|
collabs = client.folder_collaborations(item).select do |collab|
collab['item']['id'] == item['id']
end
collabs.each do |collab|
puts ["#{path}/#{item['name']}",collab['accessible_by']['name'],collab['accessible_by']['login'],collab['role']].join("\t")
end
if collabs.empty?
puts "#{path}/#{item['name']}\tNO COLLABORATORS"
end
list_collabs(path+'/'+item['name'],item,client)
end
end
# e.g. '/hathitrust-whatever'
path = ARGV[0]
list_collabs(path,client.folder_from_path(path),client)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment