以前,我开发了一个应用程序,可以从公司的Sharepoint网站下载文件,然后对其进行一些魔术操作。
自从迁移到MS Teams之后,我已经拥有了强大的功能,我正在尝试更新该应用程序以使用新平台。但是,下载文件时遇到各种问题。
我以前使用的代码(用于Sharepoint)使用WebClient
根据用户先前提供的凭据检索文件:
private string GetSchedule(string username, string password, string domain)
{
string tempPath = Path.GetTempFileName().Replace(".tmp", ".xlsm");
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(username, password, domain);
try
{
client.DownloadFile(_networkSchedulePath, tempPath);
}
catch (WebException e)
{
if (e.Message.Contains("401"))
{
StatusUpdated?.Invoke(this, new EventArgs<string>("Invalid Credentials Provided"));
Finished?.Invoke(this, null);
return null;
}
if (e.Message.Contains("404"))
{
StatusUpdated?.Invoke(this, new EventArgs<string>("File Not Found"));
Finished?.Invoke(this, null);
return null;
}
else
{
StatusUpdated?.Invoke(this, new EventArgs<string>(e.Message));
Finished?.Invoke(this, null);
return null;
}
}
}
return tempPath;
}
但是,当我在新团队链接中使用它时,出现了403 Forbidden
错误。那么有什么方法可以以编程方式从MS Teams检索文件?
答案 0 :(得分:0)
感谢JLRishe的帮助,他提供了答案和评论。但是,最终的解决方案与他的回答不同,这就是为什么我将其发布在这里的原因:
public void Main()
{
// Generate 100 items and 'process' async
Observable
.Range(0, 100)
.Select(x => ProcessItemAsync(x))
.Concat()
.Dump();
}
private readonly Random _rnd = new Random();
public async Task<int> ProcessItemAsync(int item)
{
await Task.Delay(_rnd.Next(0, 1000));
return item;
}
软件包已广泛用于此目的。
首先,根据需要访问的特定共享点站点,使用OfficeDevPnP.Core
获取AuthenticationManager
。这将弹出一个窗口以允许使用MFA。然后,通过ClientContext
对象加载各种组件。从这里,文件通过ClientContext
提取并转储到磁盘。
Guid
答案 1 :(得分:0)
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System.IO;
using System.Linq;
namespace Answer
{
class Answer
{
static void Main(string[] args)
{
// Create Confidential Application
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create("<My_Azure_Application_Client_ID>")
.WithTenantId("<My_Azure_Tenant_ID>")
.WithClientSecret("<My_Azure_Application_Client_Secret>")
.Build();
// Create an authentication provider.
ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);
// Configure GraphServiceClient with provider.
GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider);
// Get a user
var user = graphServiceClient.Users["<My_Azure_User_Name>"].Request().GetAsync().Result;
// Get the teams the user is member of
var joinedTeams = graphServiceClient.Users[user.Id].JoinedTeams.Request().GetAsync().Result;
// Get the team we are intereseted in
var team1 = joinedTeams.FirstOrDefault(t => t.DisplayName == "<TeamName_Of_Interest>");
// Get the main folders
var folders = graphServiceClient.Groups[team1.Id].Drive.Root.Children
.Request()
.GetAsync().Result;
// Get the files in the first main folder
var files = graphServiceClient.Groups[team1.Id].Drive.Items[folders[0].Id].Children
.Request()
.GetAsync().Result;
// Get the file-Data of the first file
MemoryStream fileData = graphServiceClient.Groups[team1.Id].Drive.Items[files[0].Id].Content
.Request()
.GetAsync().Result as MemoryStream;
// Save the file to the hard-disc
System.IO.File.WriteAllBytes($"C:\\{files[0].Name}", fileData.ToArray());
}
}
}