Last active
April 21, 2021 17:11
-
-
Save aelkiss/5a7119a36cc6570041a9493903d2020c to your computer and use it in GitHub Desktop.
List external collaborators using the box API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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