Accessing Collaboration object from Python SDK?
回答済みI'm trying to add collaborators to a folder using the python SDK and the collaboration object. Support for the collaboration object is not built in to the python SDK, but the SDK writers helpfully note that you can construct other requests using something like:
json_response = client.make_request( 'GET', client.get_url(parameter, parameter, parameter), ).json()
However, I don't know how to get from the kind of collaboration request I want to send:
curl https://api.box.com/2.0/collaborations \
-H "Authorization: Bearer ACCESS_TOKEN" \
-d '{"item": { "id": "FOLDER_ID", "type": "folder"}, "accessible_by": { "id": "USER_ID", "type": "user" }, "role": "editor"}' \
-X POST
to the correct client.make_request() or client.get_url() syntax. In particular, I don't know how to pass in the item and accessible_by parameters, and as far as I can tell, the collaboration object doesn't actually have a get_url method.
Thank you very much for any help you can provide!
-
I just attended the biweekly Developer Office Hours, and learned many exciting things.
ONE, the Collaboration object is already supported by the Python SDK, it's just not documented. You just have to search the github repo to see that Collaboration object support is defined here, and the add_collaborator function is defined in folder.py.
A usage example is in example.py:
root_folder = client.folder(folder_id='0') collab_folder = root_folder.create_subfolder('collab folder') try: print('Folder {0} created'.format(collab_folder.get()['name'])) collaboration = collab_folder.add_collaborator('***email address removed for privacy***', CollaborationRole.VIEWER) print('Created a collaboration')
TWO, even is not sure how to use the make_request() function with all the parameters that the Collaboration object requires, but I will post that here once he gets back to me.
-
I had a relevant similar problem and wanted to share my code in case someone is struggling with something similar.
A few weeks after we created all these collaborations, we wanted to cut off any that were still pending. (We wanted to cut these off because a deadline had passed, and we didn't want people to get the automated "final reminder" from Box to accept the collaboration invite.)
This is hard in the python SDK because there isn't a method to list all existing collaborations on a folder, and there isn't a method to create a collaboration object from a collaboration ID, at least that I could find.
folder_id = 'nnnnnnnn' root_folder = client.folder(folder_id=folder_id) items = root_folder.get_items(limit=200, offset=0) for i,item in enumerate(items): json_response = client.make_request( 'GET', client.get_url('folders', item.id, 'collaborations'), ).json() for collaboration in json_response['entries']: if collaboration['status'] == "pending": print "Folder: %s, %s, created by %s at %s" % (item.name, collaboration['status'], collaboration['created_by']['name'], collaboration['created_at']) delete_me = Collaboration(client._session, object_id=collaboration['id'],response_object=collaboration) delete_me.delete()
The code above identifies all subfolders of folder nnnnnnnn, identifies all collaborations, and then prints and deletes any that are still pending.
In order to make delete_me = Collaboration(client._session, object_id=collaboration['id'],response_object=collaboration) work, you need from boxsdk.object.folder import Collaboration
サインインしてコメントを残してください。
コメント
2件のコメント