Unable to create a shared link using the Box API V2

偶尔善良 提交于 2019-12-13 04:40:29

问题


UPDATE: I figured it out and posted the answer below.

All I'm trying to do is update any file attribute. Description, name, anything, but no matter how I format it I get a 403.

I need to be able to modify a file so it can be shared via the Box API from a cloud app. I'm updating someone else's code from V1, but they are no longer available... I've tried many things but mostly just get 403 Forbidden errors.

There are no issues with OAuth2, that works fine and I can list files and folders, but can not modify them. This question is about sharing, but I can't change a description either. The box account is mine and I authenticate with my admin credentials. Any suggestions would be appreciated.

Here is the method I am using. I pass in the fileId and token and I've left out try/catch etc. for brevity.

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

Thanks.


回答1:


Okay, My Homer Simpson moment...

UploadData is a POST, I needed to do a PUT. Here is the solution.

        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:


try changing your content type to 'multipart/form-data'?

I just looked up the api at: https://developers.box.com/docs/#files-upload-a-file

and it looks like the server is expecting a multipart post

here is stack overflow post on posting multipart data:

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



来源:https://stackoverflow.com/questions/23037138/unable-to-create-a-shared-link-using-the-box-api-v2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!