Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I upload a file to a personal OneDrive account? #696

Open
chriswilding opened this issue Apr 24, 2024 · 1 comment
Open

How can I upload a file to a personal OneDrive account? #696

chriswilding opened this issue Apr 24, 2024 · 1 comment

Comments

@chriswilding
Copy link

chriswilding commented Apr 24, 2024

Hi, I'm working on a service that will automatically upload files that a user has requested periodically to their OneDrive. While I've managed to get this working for users with a Microsoft 365 account, I haven't been able to successfully upload a file for users with a personal Microsoft account / to their personal OneDrive.

Does the this SDK support uploading files to a personal OneDrive, and if so how would I do that?

A short example of what I have working for a Microsoft 365 account is in this repository - https://github.com/chriswilding/upload-file-to-onedrive.

The same technique does not work for a personal OneDrive. With a Microsoft 365 OneDrive account I was only able to upload a file in a two step process, first PUTing the file, and the POSTing the content. With a personal OneDrive account both setting and not setting the content results in an error.

         name := "example.txt"
	contentType := "text/csv"
	data := []byte("Hello,World!")

	newFile := models.NewFile()
	newFile.SetMimeType(&contentType)

	fileItem := models.NewDriveItem()
	fileItem.SetName(&name)
	fileItem.SetAdditionalData(map[string]interface{}{
		"@microsoft.graph.conflictBehavior": "replace",
	})
	fileItem.SetFile(newFile)

	// When trying to upload to a personal OneDrive account, if the content is set the a error is returned -
	// Error creating file item: [child] A stream property 'content' has a value in the payload. In OData, stream property must not have a value, it must only use property annotations.
	// error: invalidRequest
	// error: [child] A stream property 'content' has a value in the payload. In OData, stream property must not have a value, it must only use property annotations.
	//
	// However, if the content is not set then a different error is returned -
	// Error creating file item: Cannot create a file without content
	// error: invalidRequest
	// error: Cannot create a file without content
	// fileItem.SetContent(data)

	createdFileItem, err := client.Drives().ByDriveId("aDriveID").Items().
		ByDriveItemId("aParentDriveItem").Children().
		Post(context.Background(), fileItem, nil)
@prochac
Copy link

prochac commented Sep 1, 2024

If OneDrive is the only thing you are interested in, try this module:
https://github.com/goh-chunlin/go-onedrive

This "SDK" is just a trash pile of generated Go files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants