我构建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();
我试图找到一个文件
graphClient.customRequest("/drive/root/search(q='file-name')")
.buildRequest()
.get();
但是收到403 HTTP状态,这是作用域或文件权限的问题吗?
UDP:我也尝试
DriveItemSearchCollectionPage search = graphClient
.drive()
.root()
.search(DriveItemSearchParameterSet.newBuilder() .withQ("File_Name").build())
.buildRequest()
.get();
响应:
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
看起来您使用了错误的代码。使用此代码搜索文件:
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
DriveItemSearchCollectionPage search = graphClient.me().drive().root()
.search(DriveItemSearchParameterSet
.newBuilder()
.withQ("File_Name")
.build())
.buildRequest()
.get();
参考:https://learn.microsoft.com/en-us/graph/api/driveitem-search?view=graph-rest-1.0&tabs=java
需要添加作用域"Sites.Read.All";在更新之前;User.Read.All";以及";文件.读取.全部";