Upload a file on one drive using microsoft graph api in c# asp.net

Alisha Mulla 5 Reputation points
2023-02-02T06:06:01.9066667+00:00

Hello,

I have one question,I want to upload a file on one drive and it is worked properly in console application but not  in web application. Please help me

here below is my sample console application code.

<--------------------------------------------------------------------------------------->

 var clientID = "
            var clientSec = "****";
            var tenant = "****";
            var userID = "***";

            var filePath = @"C:\onedrivedoc\LRN DRAFT retail (1).docx";
            var fileName = "LRN DRAFT retail (1).docx";

            IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                .Create(clientID)
                .WithTenantId(tenant)
                .WithClientSecret(clientSec)
                .Build();

            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
            var graphClient = new GraphServiceClient(authProvider);

            StreamReader reader = System.IO.File.OpenText(filePath);

            var uploadedFile = graphClient.Users[userID].Drive.Root.ItemWithPath(fileName).Content.Request().PutAsync
Microsoft 365 and Office | SharePoint | For business | Windows
Developer technologies | ASP.NET | Other
{count} vote

4 answers

Sort by: Most helpful
  1. Carlos Solís Salazar 18,191 Reputation points MVP Volunteer Moderator
    2023-02-07T13:25:23.56+00:00

    To grant Permission for your App registration, you require to follow the next steps:

    1. In the navigation pane, click API permissions.
    2. Click Add a permission. The Request API permissions pane appears.
    3. Click Microsoft Graph tile.
    4. Click Application permissions.
    5. Select the following permissions:
      • Directory: Directory.Read.All
      • Files: Files.ReadWrite.All
      • User: User.Read.All
      • Notes: Notes.ReadWrite.All
      • Application: Application.ReadWrite.All
    6. Click Add permissions.
    7. Click Add a permission. The Request API permissions pane appears.
    8. Click Grant admin consent for tenant_name.
    9. Click Yes.

    Hope this helps!


    Accept Answer and Upvote, if any of the above helped, this thread can help others in the community looking for remediation for similar issues.

    NOTE: To answer you as quickly as possible, please mention me in your reply.


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Shakti Rai 0 Reputation points
    2024-08-06T20:53:16.21+00:00

    hi I am trying to save the file in OneDrive but getting an error in the below cord.

            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
    
            var graphClient = new GraphServiceClient(authProvider);
    
    0 comments No comments

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.