gpt4 book ai didi

c# - 使用SSIS将文件上传到共享点文档库时获取远程服务器返回错误: (403) Forbidden.错误

转载 作者:行者123 更新时间:2023-12-05 06:10:49 25 4
gpt4 key购买 nike

我想使用 SSIS 将文件从本地目录上传到 Share Point 文档库。我正在使用脚本任务来这样做。运行包时出现以下错误:

The remote server returned an error: (403) Forbidden.

脚本任务代码如下:

public void Main()
{
// TODO: Add your code here
WebClient myWebClient;
string DestinationURL;
string LocalFileName;
bool FireAgain = true;
try
{
myWebClient = new WebClient();
myWebClient.Headers.Add("user-agent", "Only a test!");
LocalFileName = @"C:\Users\jay.desai\Desktop\LSRSQL01_ACXM_20201003.html";
DestinationURL = @"https://companyname.sharepoint.com/sites/DataServices/Shared%20Documents/Data%20Dictionaries/LSRSQL01_ACXM_20201003.html";
Console.WriteLine(LocalFileName);
myWebClient.Credentials = new NetworkCredential("jay.desai@companyname.com", "HelloWorld@2891", "NATSYS");
myWebClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
//myWebClient.UseDefaultCredentials = true;
Dts.Events.FireInformation(0, String.Empty, String.Format("Uploading {0} to {1}", LocalFileName, DestinationURL), String.Empty, 0, ref FireAgain);
// upload the file
myWebClient.UploadFile(DestinationURL, "PUT", LocalFileName);
}
catch (Exception ex)
{
// Catch and handle error
Dts.Events.FireError(0, String.Empty, ex.Message, String.Empty, 0);
}

Dts.TaskResult = (int)ScriptResults.Success;
}

我可以打开同一个文件夹并使用浏览器上传文件。

最佳答案

使用 CSOM 解决了这个问题。为此,您首先需要安装 SharePoint Online 客户端组件 SDK。然后在项目中您必须引用 Microsoft.SharePoint.ClientMicrosoft.SharePoint.Client.Runtime。 Microsoft 引用是 here

在脚本任务中使用以下代码:

public void Main()
{
// TODO: Add your code here
ClientContext clientContext = new ClientContext("https://companyname.sharepoint.com");

SecureString passWord = new SecureString();
foreach (char c in "HelloWorld@1234".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("jay.desai@company.com", passWord);

using(FileStream fileStream = new FileStream(@"C:\Users\jay.desai\Desktop\LSRSQL01_ACXM_20201003.html", FileMode.Open))
ClientOM.File.SaveBinaryDirect(clientContext, "/sites/DataServices/Shared Documents/Data Dictionaries/LSRSQL01_ACXM_20201003.html", fileStream, true);

Dts.TaskResult = (int)ScriptResults.Success;
}

关于c# - 使用SSIS将文件上传到共享点文档库时获取远程服务器返回错误: (403) Forbidden.错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64253182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com