Спасибо, Ян, ваше предложение оказалось полезным.
Я выполнил те же шаги и использовал приведенный ниже код, и теперь я могу получить данные с сайта SPO с помощью токена Azure.
static void ConnectToSPO()
{
string SiteURL = "https://SPOSite.sharepoint.com/";
#region Obtain token
AuthenticationResult result = null;
// first, try to get a token silently
try
{
result = authContext.AcquireTokenSilentAsync(SiteURL, clientId).Result;
}
catch (AggregateException exc)
{
AdalException ex = exc.InnerException as AdalException;
// There is no token in the cache; prompt the user to sign-in.
if (ex != null && ex.ErrorCode != "failed_to_acquire_token_silently")
{
// An unexpected error occurred.
ShowError(ex);
return;
}
}
if (result == null)
{
UserCredential uc = TextualPrompt();
// if you want to use Windows integrated auth, comment the line above and uncomment the one below
// UserCredential uc = new UserCredential();
try
{
result = authContext.AcquireTokenAsync(todoListResourceId, clientId, uc).Result;
}
catch (Exception ee)
{
ShowError(ee);
return;
}
}
#endregion
#region Get SharePoint Online Context & Access SPO Data
using (ClientContext ctx = TokenHelper.GetClientContextWithAccessToken(SiteURL, result.AccessToken))
{
try
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("");
Console.WriteLine("*****************************************************************************");
Console.WriteLine("Connecting To SPO Site: " + SiteURL);
ctx.Load(ctx.Web);
ctx.ExecuteQuery();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Connected !");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Info: Site Name-> " + ctx.Web.Title);
ctx.Load(ctx.Web.CurrentUser);
ctx.ExecuteQuery();
Console.WriteLine("Info: Current User Login Name-> " + ctx.Web.CurrentUser.LoginName);
#region Read List Items
Console.WriteLine("");
Console.WriteLine("Info: Reading list items from list Test List");
List testlist = ctx.Web.Lists.GetByTitle("Test List");
CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
ListItemCollection items = testlist.GetItems(query);
ctx.Load(items);
ctx.ExecuteQuery();
foreach (ListItem listItem in items)
{
// We have all the list item data. For example, Title.
Console.WriteLine(listItem["Title"]);
}
Console.WriteLine("");
#endregion
}
catch (Exception ex)
{
ShowError(ex);
}
}
#endregion
}
Обратите внимание:
Чтобы получить токен доступа для Azure, я использовал приведенный ниже код статьи.
active-directory-dotnet-nativeбез головы
Ниже приведены шаги, которые я выполнил:
Выполнены шаги, упомянутые в статье
Добавлены разрешения доступа Office 365 Sharepoint Online API для приложения.
Выбраны необходимые разрешения для приложения.
Ииспользовал упомянутый выше код для извлечения данных из SPO SIte.