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.
amit patilPosted Feb 28, 2024, 2:54 PM
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.
Mithilesh TataPosted Feb 28, 2024, 7:06 AM
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:
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: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.Jayraj ChhayaPosted Feb 27, 2024, 2:04 PM
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:
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.