有没有办法通过C#上传文件到共享的在线OneDrive

问题描述 投票:0回答:0

我在弄清楚如何通过 C# 控制台应用程序将一组文件上传到共享的在线 OneDrive 时遇到了问题。我尝试了多种不同的解决方案,但没有任何效果。我想我在这件事上走错了路。

修改链接OneDrive: 共享 OneDrive 链接

这是我用来上传文件的代码,但它一直抛出错误

Microsoft.SharePoint.Client.ClientRequestException:无法联系指定 URL 的站点...

在 Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfo(WebRequestExecutor 执行器) 在 Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate() 在 Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest() 在 Microsoft.SharePoint.Client.ClientContext.ExecuteQuery() 在 API_Calls.ACH_Upload() 中

文件路径:第 70 行

public class API_Calls
{
    public static void ACH_Upload()
    {
        try
        {
            string userName = "username";
            string password = "password";
            var securePassword = new SecureString();

            foreach (char c in password)
            {

                securePassword.AppendChar(c);
            }

            using (var ctx = new ClientContext("OneDrive_Url"))
            {
                ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(userName, securePassword);
                Web web = ctx.Web;
                ctx.Load(web);
                ctx.ExecuteQuery();
                FileCreationInformation newFile = new FileCreationInformation();
                newFile.Content = System.IO.File.ReadAllBytes(@"C:\Data\ACH_Files\ACH_" + DateTime.Now.ToString("MM_dd_yyyy") + @"\" + DateTime.Now.ToString("MM_dd_yyyy") + @"_ACH_organization.xlsx");
                newFile.Url = DateTime.Now.ToString("MM_dd_yyyy") + @"_ACH_organization.xlsx";
                List byTitle = ctx.Web.Lists.GetByTitle("ACH_" + DateTime.Now.ToString("MM_dd_yyyy"));
                Folder folder = byTitle.RootFolder.Folders.GetByUrl("_developmentfoldername");
                ctx.Load(folder);
                ctx.ExecuteQuery();
                Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(newFile);
                ctx.Load(byTitle);
                ctx.Load(uploadFile);
                ctx.ExecuteQuery();
                Console.WriteLine("done");
            }

        }

        catch (Exception e)
        {

            Console.WriteLine(e.ToString());
        }
 }
}

我希望它能读取本地文件夹中的文件并将该文件上传到我在共享 OneDrive 文件夹中创建的文件夹,但如上所示,它不起作用。

我是在走正确的道路以获得正确的解决方案,还是完全偏离了?提前谢谢大家。

c# sharepoint console-application onedrive
© www.soinside.com 2019 - 2024. All rights reserved.