'How to get Sharepoint authorization cookie from selenium and use it in Rest API?

I'm using selenium browser to automate the log-in process for Sharepoint(my tenant does not allow easy access so this is the only way). I want to get the auth cookie from the selenium browser and use it in a seperate sharepoint API.

I'm not sure if just the FedAuth cookie will be enough for the authorization but I can't seem to test it since my console app has some issues with using ASync. My console application stops without finishing the entire function.

My code:

static async Task Main(string[] args)
        {
            async Task collectDataWithCookie()
            {
                {
                string FedCookie = Sharepointdriver.Manage().Cookies.GetCookieNamed("FedAuth").ToString();
                    var baseAddress = new Uri("https://-my.sharepoint.com/personal/_api/files");
                    var cookieContainer = new CookieContainer();
                    using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
                    using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
                    {
                        var request = new HttpRequestMessage(HttpMethod.Get, "https://tenant-my.sharepoint.com/personal/tenant/_api/files");
                        request.Headers.Add("Cookie", FedCookie);
                        var result = await client.SendAsync(request);
                        string response = request.Content.ReadAsStringAsync().Result;
                    }
                }
            }
    }

How do I get Sharepoint auth token/cookie from selenium webbrowser and use it for Sharepoint rest API?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source