سؤال

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.

هل كانت مفيدة؟

المحلول

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;

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top