I need your help with my small application.
I need to create easy console application which will sign into SharePoint Online and download some documents from library.
When I am doing this on older SharePoint version it works fine. Unfortunately, its not working on SharePoint Online in Microsoft 365.
string userName = "[email protected]";
string passwordPlain = "passw0rd";
string baseUrl = "https://teams.mypage.com/sites/TESTSITE/";
var passWord = new SecureString();
foreach (char c in passwordPlain)
passWord.AppendChar(c);
var creds = new SharePointOnlineCredentials(userName, passWord);
try
{
var authDiguestToken = GetRequestDigest(creds, baseUrl);
Console.WriteLine("Result " + authDiguestToken);
}
catch (Exception ex)
{
Console.WriteLine("\n\n Exception: \n\n " + ex.Message);
}
So in this example I am going to get token value and display on console.
private static string GetRequestDigest(SharePointOnlineCredentials creds, string baseUrl)
{
var client = new RestClient(baseUrl + "_api/");
var digestRequest = new RestRequest("contextinfo", Method.POST);
digestRequest.AddHeader("Accept", "application/json");
//authentication cookie
client.CookieContainer = new CookieContainer();
client.CookieContainer.SetCookies(new Uri(baseUrl), creds.GetAuthenticationCookie(new Uri(baseUrl)));
//I'm using dynamic jsut for demo, it's better to use some strongly typed class
return client.Execute<string>(digestRequest).Data;
}
So in this case I am using cookie, not sure why its not working with other SharePoint Online in Microsoft 365.
Is there something special which needs to be done to get authenticated?