【问题标题】:Unable to create a shared link using the Box API V2无法使用 Box API V2 创建共享链接
【发布时间】:2014-04-12 22:58:21
【问题描述】:

更新:我想通了并在下面发布了答案。

我要做的就是更新任何文件属性。描述,名称,任何内容,但无论我如何格式化,我都会得到 403。

我需要能够修改文件,以便可以通过云应用程序中的 Box API 共享它。我正在从 V1 更新其他人的代码,但它们不再可用......我尝试了很多东西,但大多只是得到 403 Forbidden 错误。

OAuth2 没有问题,工作正常,我可以列出文件和文件夹,但不能修改它们。这个问题是关于分享的,但我也无法更改描述。盒子帐户是我的,我使用我的管理员凭据进行身份验证。任何建议将不胜感激。

这是我正在使用的方法。我传入了 fileId 和 token,为了简洁起见,我省略了 try/catch 等。

        string uri = string.Format("https://api.box.com/2.0/files/{0}", fileId);
        string body = "{\"shared_link\": {\"access\": \"open\"}}";

        byte[] postArray = Encoding.ASCII.GetBytes(body);

        using (var client = new WebClient())
        {
            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            client.Headers.Add("Authorization: Bearer " + token);

            var response = client.UploadData(uri, postArray);

            var responseString = Encoding.Default.GetString(response);
        }

谢谢。

【问题讨论】:

    标签: c# .net cloud box-api


    【解决方案1】:

    好吧,我的荷马辛普森时刻......

    UploadData 是一个 POST,我需要做一个 PUT。这是解决方案。

            string uri = String.Format(UriFiles, fileId);
            string response = string.Empty;
            string body = "{\"shared_link\": {\"access\": \"open\"}}";
            byte[] postArray = Encoding.ASCII.GetBytes(body);
    
            try
            {
                using (var client = new WebClient())
                {
                    client.Headers.Add("Authorization: Bearer " + token);
                    client.Headers.Add("Content-Type", "application/json");
                    response = client.UploadString(uri, "PUT", body);
                }
            }
            catch (Exception ex)
            {
                return null;
            }
            return response;
    

    【讨论】:

      【解决方案2】:

      尝试将您的内容类型更改为“multipart/form-data”?

      我只是在以下位置查找了 api:https://developers.box.com/docs/#files-upload-a-file

      看起来服务器正在等待一个多部分的帖子

      这是关于发布多部分数据的堆栈溢出帖子:

      ASP.NET WebApi: how to perform a multipart post with file upload using WebApi HttpClient

      【讨论】:

      • 感谢您的回复。是的,我阅读了文档。我要做的就是: curl api.box.com/2.0/files/FILE_ID \ -H "Authorization: Bearer ACCESS_TOKEN" \ -d '{"name":"new name.jpg"}' \ -X PUT 如果我把 multipart/ form-data 或 application/json,我仍然得到 403。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 2022-12-23
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多