microsoftgraph / msgraph-sdk-android

Microsoft Graph SDK for Android! https://graph.microsoft.io
Other
51 stars 43 forks source link

Bad request when I try to create UploadSession #74

Closed sittingInFront closed 4 years ago

sittingInFront commented 6 years ago

I want to upload new content to exist file. Before I need to create UploadSession. I use code below:

UploadSession uploadSession = getGraphServiceClient()
.getMe()
.getDrive()
.getItems(existsFileID)             
.getCreateUploadSession(new DriveItemUploadableProperties())                            
.buildRequest()
.post();

but I have an error 400 bad request.

Throwable detail:                                                                               

com.microsoft.graph.http.GraphServiceException: POST https://graph.microsoft.com/v1.0/me/drive/items/8CC395F778FF3F0D!1339/microsoft.graph.createUploadSession
                                                                                                             SdkVersion : graph-android-v1.5.0
                                                                                                             Authorization : bearer EwA4A8l6BAAURSN/FHlDW5xN74t[...]
                                                                                                             {"item":{}}

                                                                                                             400 : Bad Request
                                                                                                             [...]

                                                                                                             [Some information was truncated for brevity, enable debug logging for more details]

What's wrong in my code?

UPD: When I use code below I have downloaded a new file with name "existFileID"(((

final Option uploadOption = new QueryOption("@name.conflictBehavior", "rename");
                List<Option> optionList = new ArrayList<>();
                optionList.add(uploadOption);

                try {
                    UploadSession uploadSession = getGraphServiceClient()
                            .getMe()
                            .getDrive()
                            .getRoot()
                            .getChildren(existFileID)
                            .getCreateUploadSession(new DriveItemUploadableProperties())
                            .buildRequest(optionList)
                            .post();
ChunkedUploadProvider<DriveItem> chunkedUploadProvider = new ChunkedUploadProvider<DriveItem>(uploadSession, getGraphServiceClient(), inputStream, fileSize, DriveItem.class);
                    chunkedUploadProvider.upload(Collections.singletonList(uploadOption), uploadListener, chunkSize, maxRetry);

How to upload new version of exist file?

caitlinrussell commented 6 years ago

You want to reference the folder you want to upload to, not the file you are replacing. The default behavior is to overwrite the existing file I believe.

sittingInFront commented 6 years ago

Thank you for help. It works. Can you write how I can create the request to open upload session like I saw in MS Graph API POST /me/drive/items/{itemId}/createUploadSession itemId = existfileID?

caitlinrussell commented 6 years ago

You will need to use the DriveItemUploadableProperties item to include this information. Unfortunately, this is not one of the prepopulated properties that you can set, so you will need to use the AdditionalDataManager:

DriveItemUploadableProperties properties = new DriveItemUploadableProperties();
AdditionalDataManager mgr = properties.getAdditionalDataManager();
mgr.put("@microsoft.graph.conflictBehavior", new JsonPrimitive("rename"));
sittingInFront commented 6 years ago

@cbales Can you write me some example to resumable upload existing file by ID?

caitlinrussell commented 6 years ago

Your posted code above in conjunction with the Additional data in DriveItemUploadableProperties that I posted will work for a file by ID or a folder. However, when trying to run this code for you, I just realized there are two bugs in the SDK that are preventing you from being able to do this:

  1. DriveItemUploadableProperties does not inherit from IJsonBackedObject and therefore does not serialize AdditionalData
  2. All additional data with @ is treated as transient and is not serialized

I will work on getting these fixes added to the SDK.

sittingInFront commented 6 years ago

Thank you for the answer. I am looking forward when you fix it.

sittingInFront commented 6 years ago

Hi @cbales! Can you write some terms when you plan to fix this bag?

caitlinrussell commented 6 years ago

It is not currently scheduled but it should be out in the next release of the SDK.

omrikaufman commented 5 years ago

this issue also in the .net sdk

baywet commented 4 years ago

Hi everyone, Thank you for the contribution, we have moved away from an android specific SDK to a Java SDK and this repo will be archived very soon. If you're still facing the same issue with the Java SDK, feel free to re-open an issue over there. Closing.