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

upload file to onedrive through createUploadSession - itemWithPath() not found #167

Open
Pavani-Y opened this issue Jul 15, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@Pavani-Y
Copy link

Pavani-Y commented Jul 15, 2021

Hi David,
Found some posts on githud to do following for creating uploading session to upload an item to a drive using graph client. But i dont see the itemWithPath() available on root() for odataClientFactory. Can you please check on this. or suggest an alternative to CreateUploadSession, to upload large files to ondrive.
I'm referring to this api : https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online

graphClient
.users(oneDriveUserId)
.drive()
.root()
.itemWithPath("filename.pdf")
.createUploadSession(new DriveItemUploadableProperties())
.buildRequest()
.post();

Thanks,
Pavani

@Pavani-Y
Copy link
Author

Pavani-Y commented Jul 16, 2021

Tried below code with odataClientFactory . Throws invalid request error.
graphService
.users(userId)
.drive().root()
.createUploadSession(DriveItemUploadableProperties.builder().build().withName("abc.txt"), true)
.get()

Error:Bad Request
{
"timestamp": "2021-07-16T07:32:52.588+00:00",
"path": "/api/o365/v1/drives/bisht%40hpdatapro.onmicrosoft.com/createUploadSession",
"status": "BAD_REQUEST",
"error": "Bad Request",
"requestId": "efe58cdf-2",
"statusCode": 400,
"message": "responseCode=400 from url=https://graph.microsoft.com/v1.0/users/bisht%40hpdatapro.onmicrosoft.com/drive/root/createUploadSession, expectedResponseCode in [200, 299], message=\n{"error":{"code":"invalidRequest","message":"Invalid request","innerError":{"date":"2021-07-16T07:32:26","request-id":"97973f78-9c54-4624-a8cd-72b1f18f06a6","client-request-id":"97973f78-9c54-4624-a8cd-72b1f18f06a6"}}}"
}

@davidmoten
Copy link
Owner

Ok, thanks. The use of a request like POST /drive/root:/{item-path}:/createUploadSession is not specified in the OData metadata and I'm not sure if it was what it would look like. I'll have a think about it. In the meantime as a workaround use a custom request.

@davidmoten davidmoten added the enhancement New feature or request label Jul 17, 2021
@Pavani-Y
Copy link
Author

Thanks David. Will try for custom request.
Looking for uploading files on root and also under any folder in a drive.
POST /drives/{driveId}/items/{itemId}:/{fileName}:/createUploadSession
POST /drive/root:/{fileName}:/createUploadSession

@Pavani-Y
Copy link
Author

Pavani-Y commented Jul 22, 2021

Hi David,
I tried to create upload session using below post method.
_com.github.davidmoten.odata.client.HttpService public com.github.davidmoten.odata.client.HttpResponse post(String url, java.util.List<com.github.davidmoten.odata.client.RequestHeader> requestHeaders, String content, com.github.davidmoten.odata.client.HttpRequestOptions options)
Maven: com.github.davidmoten:odata-client-runtime:0.1.59

I'm receiving response like below from httpResponse.getText() here:
"{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.uploadSession\",\"expirationDateTime\":\"2021-07-22T17:25:42.05Z\",\"nextExpectedRanges\":[\"0-\"],\"uploadUrl\":\"https://xyz-my.sharepoint.com/personal/user_xyz_onmicrosoft_com/_api/v2.0/drives/{driveid}/items/{itemid}/uploadSession?guid='....'&overwrite=True&rename=False&dc=0&tempauth=......\"}"

And then, I'm trying to upload content using upload url received from above method. I'm using below put method to execute the uploadurl. But i receive { "responseCode": 202, "bytes": null, "text": null } in httpResponse.
com.github.davidmoten.odata.client.HttpService public abstract com.github.davidmoten.odata.client.HttpResponse put(String s,
java.util.List<com.github.davidmoten.odata.client.RequestHeader> list, java.io.InputStream inputStream, int i, com.github.davidmoten.odata.client.HttpRequestOptions httpRequestOptions)
Maven: com.github.davidmoten:odata-client-runtime:0.1.59

As per documentation, i would need to get response as below:
HTTP/1.1 202 Accepted
Content-Type: application/json

{
"expirationDateTime": "2015-01-29T09:21:55.523Z",
"nextExpectedRanges": ["26-"]
}

OR
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": "912310013A123",
"name": "largefile.vhd",
"size": 128,
"file": { }
}

Can you please check on why httpResponse.getText() is null in the response. Also please suggest if any alternative, as i would need the actual reponse text.

Thanks,
Pavani.

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

No branches or pull requests

2 participants