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

Getting contents of a file in drive using DriveItem getContent() #128

Closed
AmudhaNila opened this issue Apr 27, 2021 · 13 comments
Closed

Getting contents of a file in drive using DriveItem getContent() #128

AmudhaNila opened this issue Apr 27, 2021 · 13 comments
Labels
bug Something isn't working

Comments

@AmudhaNila
Copy link

AmudhaNila commented Apr 27, 2021

Hi David,

I am trying to get the raw contents of a file in drive using the DriveItem getContent() method. But even though the file has contents, it is null in the DriveItem class. getContent() hits a null pointer exception even though the graph API on the itemid /content works.

Optional<StreamProvider> sp = graphService
                    .drives(driveId)
                    .items(itemId)
                    .get()
                    .getContent();

Can you please confirm the way to use this method? Am I missing something?

Thanks

@davidmoten
Copy link
Owner

Please show the stack trace of the NullPointerException.

@davidmoten
Copy link
Owner

davidmoten commented Apr 27, 2021

Also put on debug logging so we can see the metadata returned with the Drive Item (I want to see the json payloads).

@davidmoten
Copy link
Owner

Looking at https://docs.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0 it appears that microsoft in their wisdom (!!!) have chosen to use non-standard metadata fields for the download url (@microsoft.graph.downloadUrl). Seeing the DriveItem json returned (or print out item.getUnmappedFields().entrySet()) would be great, thanks.

@davidmoten
Copy link
Owner

You can try your code again with 0.1.53-SNAPSHOT if you like (build latest master branch) as I've added a bit of a bandaid to use @microsoft.graph.downloadUrl if it exists (and the expected OData metadata field of content@odata.mediaReadLink is not present).

@davidmoten
Copy link
Owner

Note also from README.md: https://github.com/davidmoten/odata-client#streams (add .metadataFull() before calling .get())

@AmudhaNila
Copy link
Author

Hi David,

I tried with the 0.1.53-SNAPSHOT but still facing same issue (stack trace at the end). Attaching the Drive Item json for your ref.
driveItem.txt

When I tried calling .metadataFull() before calling get(), I get a different error though. Adding it seems to add an extra /$value at the end of the request url :

getStream returned HTTP 400
url=https://graph.microsoft.com/v1.0/drives('b%219BWQkwb6BU-FGYNtnMEvuM_f_Djq5I9MvNVLQbVWhmdpm4AZrhScTbapjoB5OoNJ')/items('01OKI7IIFLATICNLEZLVFKWT5F7TT7C2E4')/content/%24value
headers=[]
response:
{"error":{"code":"BadRequest","message":"The request URI is not valid. The segment 'content' must be the last segment in the URI because it is one of the following: $ref, $batch, $count, $value, $metadata, a named media resource, an action, a noncomposable function, an action import, a noncomposable function import, an operation with void return type, or an operation import with void return type.","innerError":{"date":"2021-04-28T12:16:40","request-id":"77c43951-83e2-4f99-a626-e072c1b541eb","client-request-id":"77c43951-83e2-4f99-a626-e072c1b541eb"}}}

Here is the stack trace of the null pointer exception without .metadataFull()

java.lang.NullPointerException: null
at com.github.davidmoten.odata.client.Path.addSegmentDelimiter(Path.java:52) ~[odata-client-runtime-0.1.49.jar:na]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ org.springframework.security.web.server.authorization.AuthorizationWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.authorization.ExceptionTranslationWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.authentication.logout.LogoutWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.savedrequest.ServerRequestCacheWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.context.SecurityContextServerWebExchangeWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.authentication.AuthenticationWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.context.ReactorContextWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.header.HttpHeaderWriterWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.config.web.server.ServerHttpSecurity$ServerWebExchangeReactorContextWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.WebFilterChainProxy [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ HTTP GET "/api/o365/v1/drives/b!9BWQkwb6BU-FGYNtnMEvuM_f_Djq5I9MvNVLQbVWhmdpm4AZrhScTbapjoB5OoNJ/items/01OKI7IIFLATICNLEZLVFKWT5F7TT7C2E4/content" [ExceptionHandlingWebHandler]
Stack trace:
at com.github.davidmoten.odata.client.Path.addSegmentDelimiter(Path.java:52) ~[odata-client-runtime-0.1.49.jar:na]
at com.github.davidmoten.odata.client.Path.addSegment(Path.java:42) ~[odata-client-runtime-0.1.49.jar:na]
at com.github.davidmoten.odata.client.internal.RequestHelper.createStreamForEdmStream(RequestHelper.java:463) ~[odata-client-runtime-0.1.49.jar:na]
at odata.msgraph.client.entity.DriveItem.getContent(DriveItem.java:499) ~[odata-client-msgraph-0.1.49.jar:na]

@davidmoten davidmoten added the bug Something isn't working label Apr 28, 2021
@davidmoten
Copy link
Owner

Thanks for the details @AmudhaNila. I'm relieved to see the content@odata.mediaReadLink field and $value should not be appended to a stream property (only media entities) so I'll fix that. I'll also fix the NPE. I assume the content@odata.mediaReadLink only turns up when .metadataFull() is added?

@davidmoten
Copy link
Owner

davidmoten commented Apr 28, 2021

I've applied the fixes in PR # 128 which has been merged.

Can you build 0.1.53-SNAPSHOT again from master and try that call again please?

@davidmoten
Copy link
Owner

I've released 0.1.53 with these fixes, can you try that version please?

@davidmoten
Copy link
Owner

I've also raised https://github.com/microsoftgraph/microsoft-graph-docs-contrib/issues/3582 to fix the graph documentation.

@AmudhaNila
Copy link
Author

Hi David,

I tested with 0.1.53 and the fix works as expected. I am now able to get the stream provider of contents of a drive item using
graphService .drives(driveId) .items(itemId) .metadataFull() .get() .getContent();

Thanks a lot.

@davidmoten
Copy link
Owner

No worries. Thanks for the report. I hadn't bumped into stream properties yet in my own use cases so was an untested path.

@davidmoten
Copy link
Owner

By the way, the need to specify metadataFull() before getting a stream is an annoying gotcha that will probably bite lots more people. I'm thinking I might default to full metadata for any media entity or entity with a stream property. I'll raise a PR for this soon, comments welcome.

davidmoten added a commit that referenced this issue May 5, 2021
…st for an entity that is a Media Entity or has a stream property #128
davidmoten added a commit that referenced this issue May 5, 2021
…137)

* default to full metadata when returning an entity via an entity request for an entity that is a Media Entity or has a stream property #128
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants