1

I build GraphServiceClient

        final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
            .clientId("todo-replace-on-own")
            .clientSecret("todo-replace-on-own")
            .tenantId("todo-replace-on-own")
            .build();

        final TokenCredentialAuthProvider tokenCredAuthProvider =
            new TokenCredentialAuthProvider(List.of("https://graph.microsoft.com/.default"), clientSecretCredential);

        final GraphServiceClient graphClient = GraphServiceClient
            .builder()
            .authenticationProvider(tokenCredAuthProvider)
            .buildClient();

and I try to find a file

graphClient.customRequest("/drive/root/search(q='file-name')")
    .buildRequest()
    .get();

but receive 403 HTTP status, is it a problem in scope or file permissions?

UDP: I also try with

        DriveItemSearchCollectionPage search = graphClient
            .drive()
            .root()
            .search(DriveItemSearchParameterSet.newBuilder() .withQ("File_Name").build())
            .buildRequest()
            .get();

Response:

Error message: Access denied

GET https://graph.microsoft.com/v1.0/drive/root/microsoft.graph.search(q='File_Name')
SdkVersion : graph-java/v5.31.0


403 : Forbidden

2 Answers2

0

It looks like you are trying with the wrong code. Use this code to search the file:

GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();

DriveItemSearchCollectionPage search = graphClient.me().drive().root()
    .search(DriveItemSearchParameterSet
        .newBuilder()
        .withQ("File_Name")
        .build())
    .buildRequest()
    .get();

Reference: https://learn.microsoft.com/en-us/graph/api/driveitem-search?view=graph-rest-1.0&tabs=java

Dada
  • 6,313
  • 7
  • 24
  • 43
vicky kumar
  • 563
  • 3
  • 11
0

Need to add the scope "Sites.Read.All" before the update I had "User.Read.All" and "Files.Read.All"