如何使用 C# SDK 访问与我共享的 Box 文件夹
How to access Box folder shared with me using C# SDK
有人使用 link 与我共享了一个 Box.com 文件夹。我需要能够使用 C# SDK 或 REST API 从他们的文件夹中下载文档。
我已经尝试了所有 3 种身份验证类型,并尝试使用 C# SDK 和 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();
有什么方法可以从通过 link 与我共享的 box 文件夹中以编程方式下载文档吗?
-- 2019 年 6 月 4 日编辑
文件夹所有者和我尝试了各种方法,API 似乎仍然不允许我查看共享文件夹的内容。文件夹所有者需要做什么才能使其可见?
根据 Box 员工的建议,我进行了以下更改。
首先是未按预期工作的代码段:
// 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);
其次,修改后的版本有效,允许我查看共享给我的文件夹并允许我遍历其内容:
// 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);
为了完整起见,这里有一段代码可以让您以编程方式访问 Box 文件夹并遍历其内容:
//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}");
}
有人使用 link 与我共享了一个 Box.com 文件夹。我需要能够使用 C# SDK 或 REST API 从他们的文件夹中下载文档。
我已经尝试了所有 3 种身份验证类型,并尝试使用 C# SDK 和 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();
有什么方法可以从通过 link 与我共享的 box 文件夹中以编程方式下载文档吗?
-- 2019 年 6 月 4 日编辑
文件夹所有者和我尝试了各种方法,API 似乎仍然不允许我查看共享文件夹的内容。文件夹所有者需要做什么才能使其可见?
根据 Box 员工的建议,我进行了以下更改。
首先是未按预期工作的代码段:
// 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);
其次,修改后的版本有效,允许我查看共享给我的文件夹并允许我遍历其内容:
// 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);
为了完整起见,这里有一段代码可以让您以编程方式访问 Box 文件夹并遍历其内容:
//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}");
}