Remove a Folder Member and Unshare it

Learn how to remove a folder member and unshare a folder using the Dropbox API.

In this lesson, we are going to explore some more endpoints of the sharing namespace, which include the following:

  • remove_folder_member
  • unshare_folder

Remove a folder member

The sharing/remove_folder_member endpoint is used to remove a specific member from a folder. Only the members with owner or editor rights can perform this task. This endpoint also needs us to enable the sharing.write permission.

Request parameters

It takes three required parameters, which are outlined in the following table.

Parameter Name

Description

Format/Value

shared_folder_id

This is the unique ID of the shared folder.

String

member

This defines which member should be removed. We can specify them with their dropbox_id or email.

We are using the email type to identify the member. We’ll write the email address of the user in the code below to remove from the SharedFolder.

leave_a_copy

This parameter tells us whether the removed member will keep a copy of the unshared folder or not.

Boolean

The code below will remove the folder member that we added in the previous lesson.

Press + to interact
import requests
import json
url = "https://api.dropboxapi.com/2/sharing/remove_folder_member"
headers = {
"Authorization": "Bearer {{TOKEN}}",
"Content-Type": "application/json"
}
data = {
"shared_folder_id": "{{FOLDER_ID}}",
"member": {
".tag":"email",
"email":"user_id@gmail.com"},
"leave_a_copy": False
}
r = requests.request("POST",url, headers=headers, data=json.dumps(data))
print(json.dumps(r.json(), indent=4))

The code above can be broken down as follows:

  • Line 12: This shows the folder ID of SharedFolder.
  • Lines 13–15: We define the member that we want to remove.
  • Line 16: We specify that the copy of this folder shouldn’t be kept in the Dropbox of the member once it has been unshared.
  • Line 19: This is the request.
  • Line 20: We print the response.

Response

This operation works in exactly the same way as share_folder in that it can be executed synchronously or asynchronously. With synchronous execution, our response is just a completion message. However, if it’s executed asynchronously, async_job_id will be returned in the response, and we’ll have to check the status of the operation like we did with share_folder and use the same endpoint.

If async_job_id is returned by the code, then we’ll use the check_job_status endpoint. We’ll replace the placeholder {async_job_id} in line 12 with the async_job_id returned by the previous block of code. It’ll produce a complete response once the operation is done.

Press + to interact
import requests
import json
url = "https://api.dropboxapi.com/2/sharing/check_job_status"
headers = {
"Authorization": "Bearer {{TOKEN}}",
"Content-Type": "application/json"
}
data = {
"async_job_id": "{async_job_id}"
}
r = requests.request("POST",url, headers=headers, data=json.dumps(data))
print(json.dumps(r.json(), indent=4))

Unshare the folder

The /sharing/unshare_folder endpoint permits the folder members with owner rights to unshare the folder. This endpoint also requires us to have the permission sharing.write enabled.

Request parameters

This endpoint takes only two parameters. Let’s see them in the following table.

Parameter Name

Description

Format/Value

Required

shared_folder_id

This is the unique ID of the shared folder that we obtained in the previous lesson.

String

Yes

leave_a_copy

If it’s set to True, then all folder members will get a copy of the folder once it has been unshared. Otherwise, its default value is set to False.

Boolean

No

The following code unshares a shared folder.

Press + to interact
import requests
import json
url = "https://api.dropboxapi.com/2/sharing/unshare_folder"
headers = {
"Authorization": "Bearer {{TOKEN}}",
"Content-Type": "application/json"
}
data = {
"shared_folder_id": "{{FOLDER_ID}}"
}
r = requests.request("POST",url, headers=headers, data=json.dumps(data))
print(json.dumps(r.json(), indent=4))

In the above code:

  • Line 12: We present the shared_folder_id parameter. We’ve assigned it the SharedFolder ID.
  • Line 15: This is the POST request.
  • Line 16: We print the response.

Response

This operation can work synchronously or asynchronously. If it’s synchronously executed, the response is just a completion message. If the execution is asynchronous, the response will be async_job_id, and we’ll have to check the status of the operation by executing the check_job_status endpoint from the previous section.

Note: After getting the complete status of the above asynchronous job, visit the preview_url again in the browser. You’ll notice that the folder’s shareable link is no longer valid.

Press + to interact
print("preview_url is: {{PREVIEW_URL}}")