0
votes

I am trying to upload a file to OneDrive using Graph SDK for .Net Core from worker service. Basically, some files are created at random time and those files needs to be uploaded to specified path on OneDrive from worker service at midnight every day. I have following information stored in appconfig.json file in application:

  • ClientID
  • ClientSecret
  • TenantID

I have checked samples on various sites but could not find how to upload files using above ID and Secret. I believe there must but some kind of authProvider that I could initialize using above ID and Secret.

I also checked miscrosoft's documentation but coudl not find any example on how to upload file using SDK with ID and Secret.

https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content?view=odsp-graph-online

Additional Point

  • Upload new file
  • Overwrite if file already exists(hope that graph already supports this)
  • file size is < 4MB
  • path format /folder1/folder2/filename.pdf

Any help would be appreciated.

1

1 Answers

0
votes

Remember to assign Application permission (Client credentials flow) to the app registration. See Application permission to Microsoft Graph.

enter image description here

You can use Client credentials provider.

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithTenantId(tenantID)
    .WithClientSecret(clientSecret)
    .Build();

ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);

Upload small file (<4M):

GraphServiceClient graphClient = new GraphServiceClient(authProvider);

using var stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes("The contents of the file goes here."));

await graphClient.Users[{upn or userID}].Drive.Items["{item-id}"].Content
    .Request()
    .PutAsync<DriveItem>(stream);

If you want to upload large file, see Upload large files with an upload session.

UPDATE:

Use Install-Package Microsoft.Graph.Auth -IncludePrerelease in Tools -> NuGet Package Manager -> Package Manager Console.

It supports both uploading a new file and replacing an existing item.

For how to get the items id, please refer to Get a file or folder.

For example, get the item id of /folder1/folder2 (have a quick test in Microsoft Graph Explorer):

GET https://graph.microsoft.com/v1.0/users/{userID}/drive/root:/folder1:/children

It will list all the children in folder1, including folder2. Then you can find item id of folder2.

UPDATE 2:

Client credentials provider is application identity while all the providers below are user identity.

Since you want to access personal OneDrive (including user identity), you could choose Authorization code provider or Interactive provider.

Authorization code provider: you need to implement interactively sign-in for your web app and use this provider.

Interactive provider: you can easily use this provider to implement interactively sign-in in a console app.

You can have a quick test with the second provider.

Please note that you should add the Delegated (personal Microsoft account) permissions into the app registration. See Delegated permission to Microsoft Graph.

enter image description here

And in this case, you should modify all graphClient.Users[{upn or userID}] to graphClient.Me in your code.

I'm afraid that you have to implement sign-in interactively auth flow to access personal OneDrive.