简体   繁体   English

使用C#和RESTSharp在包装盒上创建文件夹

[英]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. 我正在尝试使用RESTSharp在Box上创建一个简单的文件夹,但是我遇到了RESTSharp 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"} {“类型”:“错误”,“状态”:400,“代码”:“ bad_request”,“ help_url”:“ http://developers.box.com/docs/#errors”,“消息”:“可以无法解析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. 您可能还想看看最近创建的github存储库,其中一些人正在使用C#Box SDK进行协作。 https://github.com/jhoerr/box-csharp-sdk-v2 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) URL需要指向/ folder / {folder_id}(0是根文件夹的ID)
  • The folder name needs to be in the json body of the request, not a query parameter 文件夹名称必须在请求的json主体中,而不是查询参数中

I'm not that familiar with C# or RESTSharp, but I believe this code should address the two problems. 我对C#或RESTSharp不太熟悉,但是我相信这段代码应该可以解决这两个问题。

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) { 静态字符串folderCreation(字符串APIKey,字符串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;



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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