Search code examples
c#onedrive

How can i connect/login to OneDrive with username and password using C#?


I can connect to OneDrive from within the onedrive url using username and password. I am trying to put together a c# app which will login in the user. Is there some sample app which can outline the steps required ?


Solution

  • i think this will do the job.

        public async Task GetTokenAsync(string tenant, string clientId, string clientSecret, string username, string password)
    {
        HttpResponseMessage resp;
        using (var httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
            var req = new HttpRequestMessage(HttpMethod.Post, $"https://login.microsoftonline.com/{tenant}/oauth2/token/");
             req.Content = new FormUrlEncodedContent(new Dictionary<string, string>
             {
                {"grant_type", "password"},
                {"client_id", clientId},
                {"client_secret", clientSecret},
                {"resource", "https://graph.microsoft.com"},
                {"username", username},
                {"password", password}
             });
    
             resp = await httpClient.SendAsync(req);
             string content = await resp.Content.ReadAsStringAsync();
             var jsonObj = new JavaScriptSerializer().Deserialize<dynamic>(content);               
             string token = jsonObj["access_token"];                
             Console.WriteLine(token);
        }
    }