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

时间:2019-05-30 12:48:04

标签: c# box-api

有人使用该链接与我共享了Box.com文件夹。我需要能够使用C#SDK或REST API从其文件夹中下载文档。

我尝试了所有3种身份验证类型,并尝试使用C#SDK和REST API进行访问。

id   su_id  r_value  match_v

A      A1      0        1
A      A2      0        1
A      A3      70       2
A      A4      120      2
A      A5      250      3
A      A6      250      3
B      B1      0        1
B      B2      30       2

是否可以通过编程方式从通过链接与我共享的文件夹中以编程方式下载文档?

1 个答案:

答案 0 :(得分:2)

根据我从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}");
}