Код: Выделить всё
string authority = $"https://login.microsoftonline.com/{tenantId}";
string filePath = @"C:\temp\todo.docx";
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(new Uri(authority))
.Build();
string[] scopes = new[] { $"{sharePointSiteUrl}/.default" };
AuthenticationResult result;
try
{
result = app.AcquireTokenForClient(scopes).ExecuteAsync().GetAwaiter().GetResult();
}
catch (MsalException ex) when (ex.Message.Contains("AADB_AUTHORIZATION_CODE_REQUIRED"))
{
// Handle user interaction for MFA flow (interactive token acquisition)
result = app.AcquireTokenForClient(scopes)
.ExecuteAsync().GetAwaiter().GetResult(); // User will be prompted for MFA through the browser
}
catch (Exception ex)
{
throw new Exception($"Error acquiring access token: {ex.Message}");
}
string fileName = "Test.docx";
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
string uploadUrl = $"{sharePointSiteUrl}_api/web/GetFolderByServerRelativeUrl('{SharePointInternalGroupName}/{SharePointLibraryFolder}')/Files/add(url='{fileName}',overwrite=true)";
byte[] fileBytes = File.ReadAllBytes(filePath);
using (var content = new ByteArrayContent(fileBytes))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
try
{
var response = httpClient.PostAsync(uploadUrl, content).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
return true;
}
catch (HttpRequestException ex)
{
string errorContent = ex.Message;
}
}
}
}
catch (Exception exp)
{
throw exp;
}
Код: Выделить всё
var response = httpClient.PostAsync(uploadUrl, content).GetAwaiter().GetResult();
"Произошла ошибка при проверке подлинности запроса.";category="invalid_client"
разрешение, установленное на сервере, выглядит следующим образом:

В токене доступа я не могу найти никаких утверждений о ролях, я не знаю, может ли это может быть какая-то причина, но что может помешать мне загрузить файл в SharePoint?
Подробнее здесь: https://stackoverflow.com/questions/793 ... lid-client
Мобильная версия