Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
demo22
8 years agoExplorer | Level 3
Unable to use the add_file_member API (HTTP) with C#
I am trying to use this API but when inputting the parameters by creating anonymous properties, thanks to the "dot" in the ".tag" in "members", I cannot create that property as it will ask for an identifier in C#. Hence I am not able to create the request successfully. Please suggest an example for making an HTTP request on this API in C#. Thank you.
11 Replies
- Greg-DB8 years ago
Dropbox Community Moderator
If you're trying to use the Dropbox API from C#, we highly recommend using the official Dropbox .NET SDK, as it will do a lot of the work for you, such as building these objects:
https://github.com/dropbox/dropbox-sdk-dotnet
If you can't use that for whatever reason, we'll be happy to help with any issues you're having with the Dropbox API, but I'll need some more information. Please reply with:- the name and version of the platform and Dropbox or HTTP library you are using, if any
- the full text of the error/any output
- the relevant code snippet(s) you're stuck on
- demo228 years agoExplorer | Level 3
Thank you for your reply, I am currently using Nemiro.OAuth. library and I face the problem when using "https://api.dropboxapi.com/2/sharing/add_file_member" API. I need to create anonymous properties for the parameters in this API, but the ".tag" under "members" in the parameters has refused to let me create a property for it.
The following code should work if the parameter does not have a "dot".
public static void ShareFile(string id, string account, string level)
{
var re = OAuthUtility.Post
(
"https://api.dropboxapi.com/2/sharing/add_file_member",
parameters:new HttpParameterCollection
{
new
{
file = id,
members = new[]
{
new
{
.tag = "email",
email = account
}
},
access_level = level
}
},
authorization: Authorization,
contentType: "application/json"
);
} - Greg-DB8 years ago
Dropbox Community Moderator
Thanks for following up with the additional information. I'm afraid I don't have a good solution to offer. The Dropbox API does use the key name ".tag" to identify the subtype of a struct or selected member of a union. I'll pass this along as a feature request for an alternative, but that's unlikely to be done in this version of the API, as that would be a significant breaking change.
That being the case, you can try not using anonymous properties, or use the official Dropbox API v2 .NET SDK instead.
- demo228 years agoExplorer | Level 3
Thanks for the reply but I want to know how you expect developers to use this HTTP request in C# instead? I mean when you design this API you must have thought of this problem right? Is it possible that you suggest a C# function that can use this API? I don't want to use the .NET SDK just because one API is not working while other APIs are working fine. Thank you
- Greg-DB8 years ago
Dropbox Community Moderator
Thanks for the feedback! I'll be sure to pass it along to the team. I can't offer help with Nemiro in particular, but if you're not using the official SDK, the expected use pattern would be that you would build the JSON to send in the request body, e.g., using Json.NET or the like, where you can use arbitrary key names. For instance, you would use `JsonConvert.SerializeObject` and pass in a native Dictionary with any key names, where ".tag" would work.
- demo228 years agoExplorer | Level 3
Thank you for your update. It is ok not to use Nemiro but can you make a simple sample function for this API in C# to make it more specific? Because I have tried lots of ways to insert the parameters but still no luck. For most of the cases I got is the error "Error in call to API function "sharing/add_file_member": request body: expected object, got string" or "Error in call to API function "sharing/add_file_member": request body: expected object, got list". Thank you for your help.
- Greg-DB8 years ago
Dropbox Community Moderator
Here's an example of one way you might do this:
WebRequest request = WebRequest.Create("https://api.dropboxapi.com/2/sharing/add_folder_member"); request.Method = "POST"; request.Headers.Add("Authorization", "Bearer " + accessToken); request.ContentType = "application/json"; Stream requestBodyStream = request.GetRequestStream(); JsonSerializerSettings serializerSettings = new JsonSerializerSettings(); serializerSettings.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii; var memberSelector = new Dictionary<String, String>() { { ".tag", "email" }, { "email", "email@example.com" } }; var accessLevel = new Dictionary<String, String>() { { ".tag", "editor" } }; var member = new Dictionary<String, Dictionary<String, String>>() { { "member", memberSelector }, { "access_level", accessLevel } }; var members = new List<Dictionary<String, Dictionary<String, String>>>() { member }; var addFolderMemberArgs = new Dictionary<String, dynamic> { { "shared_folder_id", "248930168" }, { "members", members } }; var addFolderMemberArgsString = JsonConvert.SerializeObject(addFolderMemberArgs, serializerSettings).Replace("\x7F", "\\u007f"); var addFolderMemberArgsBytes = Encoding.ASCII.GetBytes(addFolderMemberArgsString); requestBodyStream.Write(addFolderMemberArgsBytes, 0, addFolderMemberArgsBytes.Length); requestBodyStream.Close(); WebResponse response = null; try { response = request.GetResponse(); } catch (WebException e) { Console.WriteLine(e.Status); Console.WriteLine(e.Message); response = e.Response; } finally { Stream responseStream = response.GetResponseStream(); StreamReader responseReader = new StreamReader(responseStream); Console.WriteLine(responseReader.ReadToEnd()); responseReader.Close(); response.Close(); } - demo228 years agoExplorer | Level 3
Thank you for your detail example, it looks more complicated than I expected but it can send the parameters correctly. However, the result I get is an error like this:
"error_summary": "access_error/invalid_id/.."
I read the doc and I know I got the wrong folder ID, which I got from the result of this API:
https://api.dropboxapi.com/2/files/list_folder
The example of the ID from the result is like this:
"id": "id:a4ayc_80_OEAAAAAAAAAXw"
So if this ID is wrong, then which ID should I get and how can I get it through the API?
Thank you so much for your help.
- Greg-DB8 years ago
Dropbox Community Moderator
That ID with "id:" is the file/folder ID, but the add_folder_member requires the "shared folder ID", which is an integer. You can get it from /2/files/list_folder[/continue] in FolderMetadata.sharing_info.shared_folder_id, or /2/sharing/list_folders[/continue] or /2/sharing/check_share_job_status in SharedFolderMetadata.shared_folder_id.
- demo228 years agoExplorer | Level 3
Thanks for the reply. As I read the document, I know that using this API "https://api.dropboxapi.com/2/files/list_folder" can get the "parent_shared_folder_id" inside the "sharing_info". Problem is, if the folder has not been shared before, the "parent_shared_folder_id" would not possibly appear in the result. The only way it would appear is after the folder has been shared once.
That makes the situation becomes very odd becasue in order to share a folder you need a so-called "parent_shared_folder_id". But the only way to get the "parent_shared_folder_id" you have to share the folder first. This is really confusing...
So how about you tell me the correct procedures in order to share a folder correctly using the APIs with reachable parameters? Thank you.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!