Team,
I am trying to upload & download the excel files in Dotnet Core razor pages from OneDrive where it has Email as username and password with me. I have tried mulitple code snippets but no luck till time and most of it has different implementations mainly for Sharepoint or Windows projects. Please help me. TIA.

Jayraj ChhayaRank #304 · 8.2K reputationPosted Feb 27, 2024, 9:04 AM EST
Hi amit patil,
You can utilize the Microsoft Graph API, which provides endpoints for interacting with OneDrive. Here's a high-level overview of the steps involved:
Authentication: Use the Microsoft Authentication Library (MSAL) to authenticate the user with their email and password.
Access OneDrive API: Once authenticated, use the Microsoft Graph SDK to interact with OneDrive. You can upload and download files using the
DriveItemendpoints.Sample Code:
// Install Microsoft.Graph SDK // Install Microsoft.Identity.Client (MSAL) SDK // Authentication var confidentialClientApplication = ConfidentialClientApplicationBuilder .Create("{clientId}") .WithClientSecret("{clientSecret}") .WithAuthority(new Uri("https://login.microsoftonline.com/{tenantId}")) .Build(); var authResult = await confidentialClientApplication.AcquireTokenForClient(new[] { "https://graph.microsoft.com/.default" }).ExecuteAsync(); // Access OneDrive API var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => { requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); return Task.FromResult(0); })); // Upload File using (var stream = new FileStream("path_to_file", FileMode.Open)) { await graphServiceClient.Me.Drive.Root.ItemWithPath("file_name.xlsx").Content.Request().PutAsync(stream); } // Download File var downloadedFile = await graphServiceClient.Me.Drive.Root.ItemWithPath("file_name.xlsx").Content.Request().GetAsync();By following these steps and utilizing the Microsoft Graph API, you should be able to successfully upload and download Excel files from OneDrive in your Dotnet Core Razor Pages application.
Mithilesh TataRank #862 · 2.6K reputationPosted Feb 28, 2024, 2:06 AM EST
To upload and download an Excel file from OneDrive using your email username and password, you can utilize the Microsoft Graph API, which provides programmatic access to various Microsoft services, including OneDrive. Here's a general approach using R and the
msgraphrpackage to interact with the Microsoft Graph API:First, you need to install and load the
msgraphrpackage:Then, you'll need to authenticate with your Microsoft account:
# Authenticate with your Microsoft account msgraph_auth( app = "your_app_name", tenant = "your_tenant_id", username = "your_email", password = "your_password" )Replace
"your_app_name","your_tenant_id","your_email", and"your_password"with your specific details.Once authenticated, you can use the
msgraphrfunctions to interact with OneDrive. Here's how you can upload and download an Excel file:# Upload an Excel file to OneDrive upload_file( file = "path_to_your_file.xlsx", folder = "Documents", # Destination folder in OneDrive overwrite = TRUE # Overwrite if file already exists ) # Download an Excel file from OneDrive download_file( file_id = "your_file_id", path = "path_to_save_downloaded_file.xlsx" )Replace
"path_to_your_file.xlsx","Documents", and"your_file_id"with your file's path, the destination folder in OneDrive, and the file ID of the file you want to download, respectively.Remember to handle sensitive information securely, such as storing credentials in a safe manner. Additionally, you may need to set up an Azure AD application and register it to obtain the necessary authentication parameters. Please refer to the documentation for
msgraphrand Microsoft Graph API for more details and advanced usage.amit patil27 reputationPosted Feb 28, 2024, 9:54 AM EST
Thanks for your replies Jayraj & Mithilesh.
I tried below code from Jayraj and found that, DelegateAuthenticationProvider is no longer supported in latest versions, so had to do workaround for a token. Also in "graphServiceClient.Me.Drive.Root" a Root option is not available so had to use below snippet to get the drive & other details,
DriveItem itemRoot = await graphClient.Drives["driveId"].Root.GetAsync((conf) => driveItems = itemRoot.Children;
{
conf.QueryParameters.Expand = new string[] { "children" };
}).ConfigureAwait(false);
List
But I am facing the below error while executing the async call,
Azure.Identity.AuthenticationFailedException: 'DeviceCodeCredential authentication failed: Verification code expired before contacting the server'
The Tenant Id & Client Id, I am using from my app which is used to log into the Azure. Is this correct? Or I have to create the new client id for the same. Thanks.