3

Someone has shared a Box.com folder with me using the link. I need to be able to use the C# SDK or REST API to download the documents from their folder.

I have tried all 3 authentication types and have attempted to access with both the C# SDK and REST API.

//SDK attempt
var findFolder = await client.SharedItemsManager.SharedItemsAsync("https://<userWhoSharedWithMe>.box.com/s/<folderHash>");  // notFound
var folder = await client.FoldersManager.GetInformationAsync(findFolder.Id); 
var items = folder.ItemCollection;

//API Attempt
var client = new HttpClient
{
    BaseAddress = new Uri("https://api.box.com")
};
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "<bearerToken>");

var response = await client.GetAsync("2.0/folders/<folderId>/items");
var content = await response.Content.ReadAsStringAsync();

Is there any way to programmatically download documents from a box folder that was shared with me via link?

-- Edited 06/04/2019

The folder owner and I have tried various things and it seems the API still will not allow me to see the content of the shared folder. Is there anything the folder owner needs to do to make it visible?

Daniel
  • 63
  • 6
  • This might be helpful - https://stackoverflow.com/a/11076910/969613 – JMK May 30 '19 at 13:07
  • Thanks, i have tried connecting that way, however it seems the folder is not visible to me over the API. Is there a way the folder owner can make it visible? – Daniel Jun 04 '19 at 13:31
  • @Daniel - were you able to find a solution for this? I'm in the same boat! If you were able to figure it out or even come up with an alternate approach, I'd appreciate it if you can share it. – Tom Vaidyan Feb 04 '20 at 16:39
  • @TomVaidyan - Sorry I missed this, unfortunately I was not able to resolved this at that time and had to pivot to a different approach. – Daniel Jun 17 '20 at 17:23
  • 1
    @Daniel - no worries! I have updated this question with my own answer, in case you or someone else that stumbles on this post go through the same issues that we went through. Cheers! – Tom Vaidyan Jun 18 '20 at 13:55

1 Answers1

3

Based on the suggestion that I received from a Box employee, I made the following changes.

First the snippet that didn't work as expected:

// DOES NOT WORK

var reader = new StreamReader("box-config.json");
var json = reader.ReadToEnd();
var config = BoxConfig.CreateFromJsonString(json);

var sdk = new BoxJWTAuth(config);
var token = sdk.AdminToken();
var session = new OAuthSession(token, "N/A", 3600, "bearer");

boxClient = new BoxClient(config, session, asUser: boxUserId);

Secondly, the modified version that worked, allowing me to see the folder that was shared to me and allowed me to traverse its contents:

// THIS WORKS !!!!!!!!

var reader = new StreamReader("box-config.json");
var json = reader.ReadToEnd();
var config = BoxConfig.CreateFromJsonString(json);

var sdk = new BoxJWTAuth(config);
var token = sdk.UserToken(boxUserId);
boxClient = sdk.UserClient(token, boxUserId);

And for completeness' sake, here's a snippet of code that will allow you to programmatically access a Box folder and traverse its contents:

//folderId <-- You can find this ID by logging into your box account and navigating to the folder that you're interested in accessing programmatically.

var items = await boxClient.FoldersManager.GetFolderItemsAsync(folderId, limit: 5000, offset: 0, autoPaginate: false,
    sort: "name", direction: BoxSortDirection.DESC);

// How many things are this folder?
Console.WriteLine($"TotalCount: {items.TotalCount}");

// Loop through those items
foreach (var item in items.Entries)
{
    // Get info on each item
    var file = await boxClient.FilesManager.GetInformationAsync(item.Id);

    // Print the filename
    Console.WriteLine($"file: {item.Name}");
}
Tom Vaidyan
  • 400
  • 3
  • 14