Search code examples
c#box-api

How to access Box folder shared with me using C# SDK


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?


Solution

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