简体   繁体   中英

Creating a folder on box using C# and RESTSharp

I'm trying to use RESTSharp to create a simple folder on Box, but I'm having a hard time. I keep getting this error:

{"type":"error","status":400,"code":"bad_request","help_url":"http://developers.box.com/docs/#errors","message":"Could not parse JSON","request_id":"1474540366505ba7a11bdcd"}

This is my code:

static string box(string resourceURL, string APIKey, string authToken)
        {
            RestClient client = new RestClient();
            client.BaseUrl = "https://api.box.com/2.0";
            var request = new RestRequest(Method.POST);
            request.Resource = resourceURL;
            string Headers = string.Format("Authorization: BoxAuth api_key={0}&auth_token={1}", APIKey, authToken);
            request.AddHeader("Authorization", Headers);
            request.AddParameter("name", "TestFolder");

            // request.RequestFormat = DataFormat.Json;
            var response = client.Execute(request);
            return response.Content;
        }

What am I missing? Thanks in advance for your help.

You may also want to take a look at a recently created github repo, where some folks are collaborating on a C# Box SDK. https://github.com/jhoerr/box-csharp-sdk-v2

I see two issues:

  • The URL needs to point to /folder/{folder_id} (0 is the id of the root folder)
  • The folder name needs to be in the json body of the request, not a query parameter

I'm not that familiar with C# or RESTSharp, but I believe this code should address the two problems.

static string box(string APIKey, string authToken)
            {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.box.com/2.0";
                var request = new RestRequest(Method.POST);
                request.Resource = "/folders/0";
                string Headers = string.Format("BoxAuth api_key={0}&auth_token={1}", APIKey, authToken);
                request.AddHeader("Authorization", Headers);
                request.AddParameter("text/json", "{\"name\" : \"TestFolderName\"}", ParameterType.RequestBody);

                //request.RequestFormat = DataFormat.Json;
                var response = client.Execute(request);
                return response.Content;
            }

Thanks for your help, this is the exact code that finally worked.

static string box(string APIKey, string authToken)
            {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.box.com/2.0";
                var request = new RestRequest(Method.POST);
                request.Resource = "/folders/0";
                string Headers = string.Format("BoxAuth api_key={0}&auth_token={1}", APIKey, authToken);
                request.AddHeader("Authorization", Headers);
                request.AddParameter("text/json", "{\"name\" : \"TestFolderName\"}", ParameterType.RequestBody);

                //request.RequestFormat = DataFormat.Json;
                var response = client.Execute(request);
                return response.Content;
            }

static string folderCreation(string APIKey, string authToken) {

    RestClient client = new RestClient();
    client.BaseUrl = "https://api.box.com/2.0/folders";
    var request = new RestRequest(Method.POST);
    string Headers = string.Format("Bearer {0}", authToken);
    request.AddHeader("Authorization", Headers);
    request.AddParameter("application/json", "{\"name\":\"Youka\",\"parent\":{\"id\":\"0\"}}", ParameterType.RequestBody);
    var response = client.Execute(request);
    return response.Content;



}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM