admin管理员组文章数量:1332361
I am trying to have an ASP.NET MVC app to upload file to Sharepoint, however, I got 401 error. I added access to the app API permission, not sure what else I need. It failed at context.ExecuteQuery()
.
Please advise.
// http post method
[HttpPost]
public async Task<ActionResult> UploadLargeFile(IFormFile file)
{
// Authenticate with SharePoint
var context = await GetSharePointContext();
var web = context.Web;
context.Load(web);
// Specify the target document library
var targetLibrary = web.Lists.GetByTitle("Documents");
context.Load(targetLibrary);
context.ExecuteQuery();
}
// Authentication method using Microsoft Identity Platform
private async Task<ClientContext> GetSharePointContext()
{
var app = ConfidentialClientApplicationBuilder
.Create(ClientId)
.WithTenantId(TenantId)
.WithClientSecret(ClientSecret)
.Build();
var scopes = new[] { "/.default" };
var authResult = await app.AcquireTokenForClient(scopes).ExecuteAsync();
var context = new ClientContext(SharePointSiteUrl);
context.ExecutingWebRequest += (s, e) =>
{
e.WebRequestExecutor.WebRequest.Headers["Authorization"] =
"Bearer " + authResult.AccessToken;
};
return context;
}
本文标签: cASPNET MVC upload file to sharepoint online get 401 unauthorized errorStack Overflow
版权声明:本文标题:c# - ASP.NET MVC upload file to sharepoint online get 401 unauthorized error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742318169a2452237.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论