microsoftgraph/msgraph-sdk-java

Drives appear empty resulting in NullPointerException when retriving DriveItems

Closed this issue · 2 comments

Describe the bug

It doesn't seem to matter what I try. I am able to obtain drive IDs (which I have confirmed point to a drive that has documents) using code like graphClient.users().byUserId(uid).drives().get();. But when I attempt to get the items inside those drives I get a nullPointerException, code for this looks like graphClient.drives().byDriveId(driveId).get().getItems(); or graphClient.users().byUserId(uid).drives().byDriveId(driveId).get().getItems();. I've also tried replacing the .getItems() with .getRoot().getChildren(); but that also throws a null pointer (.getRoot() is null so .getChildren() throws nullPointer).

I have confirmed that these drives have items to retrieve because I can pass the driveID into this curl call and can see the documents I am trying to retrieve, I can also see the documents when I log in to the UI.

curl --location 'https://graph.microsoft.com/v1.0/drives/[driveID]/root/children' \
--header 'Authorization: Bearer [valid_access_token]'

Expected behavior

Expected behavior is to be able to retrieve DriveItems programmatically (presumably using the code described above)

How to reproduce

Run the following code with a valid String value for clientID, clientSecret, tenantID, and uid(userId)

ClientSecretCredential credential = new ClientSecretCredentialBuilder()
        .clientId(clientID)
        .clientSecret(clientSecret)
        .tenantId(tenantID)
        .build();

    String defaultScope = "https://graph.microsoft.com/.default";
    GraphServiceClient graphClient = new GraphServiceClient(credential, defaultScope);
    DriveCollectionResponse driveCollectionResponse = graphClient.users().byUserId(uid).drives().get();

    for(Drive drive : driveCollectionResponse.getValue()){

      System.out.println("Drive ID: " + drive.getId());
      List<DriveItem> driveItemList = drive.getItems();
      System.out.println("Drive Items: " + driveItemList);

      try {
        for (DriveItem item : driveItemList) {
          System.out.println("\tItem: " + item.getName());
        }
      } catch (Exception e) {
        System.out.println("\tError: " + e.getMessage());
      }
    }
    System.out.println("Done");

SDK Version

6.19.0

Latest version known to work for scenario above?

unknown

Known Workarounds

None

Debug output

Log output from running the above code ```

Task :Main.main()
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Drive ID: [driveID]
Drive Items: null
Error: Cannot invoke "java.util.List.iterator()" because "driveItemList" is null
Drive ID: [driveID]
Drive Items: null
Error: Cannot invoke "java.util.List.iterator()" because "driveItemList" is null
Done
Error: Cannot invoke "java.util.List.iterator()" because "driveItemList" is null

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.4/userguide/command_line_interface.html#sec:command_line_warnings

</details>


### Configuration

_No response_

### Other information

_No response_

Hi @AAckerlund, thank you for reaching out.
The items are not returned in the default call to GET a drive object. However, you can try the $expand query parameter:

DriveCollectionResponse driveCollectionResponse = graphClient.users().byUserId(uid).drives().get(
	requestConfiguration -> {
		requestConfiguration.queryParameters.expand = new String[]{"items"}
	}
);

Please let me know if this works.
Also feel free to refer to our API reference docs for the API call that best suits your use-case

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.