0

我正在尝试使用RESTSharp在 Box 上创建一个简单的文件夹,但我遇到了困难。我不断收到此错误:

{"type":"error","status":400,"code":"bad_request","help_url":"http://developers.box.com/docs/#errors","message":"可以不解析 JSON","re​​quest_id":"1474540366505ba7a11bdcd"}

这是我的代码:

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;
        }

我错过了什么?在此先感谢您的帮助。

4

4 回答 4

1

您可能还想查看最近创建的 github 存储库,其中一些人正在协作开发 C# Box SDK。 https://github.com/jhoerr/box-csharp-sdk-v2

于 2012-09-23T02:47:12.270 回答
0

我看到两个问题:

  • URL需要指向/folder/{folder_id}(0是根文件夹的id)
  • 文件夹名称需要在请求的json正文中,而不是查询参数

我对 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;
            }
于 2012-09-21T02:43:13.010 回答
0

感谢您的帮助,这是最终起作用的确切代码。

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;
            }
于 2012-09-21T18:17:48.950 回答
0

静态字符串文件夹创建(字符串 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;



}
于 2014-02-25T12:56:39.093 回答