c# - 如何使用 C# SDK 访问与我共享的 Box 文件夹

标签 c# box-api

有人使用链接与我共享了一个 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();

有没有办法以编程方式从通过链接与我共享的 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}");
}

关于c# - 如何使用 C# SDK 访问与我共享的 Box 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56378630/

相关文章:

c# - 编码 LPSTR 和 float 时堆栈不平衡

c# - 线程被中止 - C# 使用队列

java - 使用 Box Android 库 (apiV2) 检查 Box 帐户中的文件/文件夹是否为只读的正确方法是什么?

c# - 使用 Mono Touch 的横向 iPad 应用程序

c# - 从 EF6 迁移到 EF Core 2.0

c# - C# 中的迭代正则表达式捕获

javascript - box api 错误获取访问 token : Invalid grant_type parameter or parameter missing

.net - Box API 2.0 下载文件

oauth-2.0 - 为什么刷新 token 会在 14 天后过期

c# - 日志记录仅在本地有效,但在部署到 Microsoft Azure 应用服务时无效