我目前正在致力于将 Microsoft Graph API 集成到我的 Java 应用程序中,以检索 Microsoft 365 租户中用户的许可证详细信息。我参考了 Microsoft 官方文档here来了解 Microsoft Graph Java SDK 为此目的的用法。然而,尽管遵循了提供的指导,但我在 Java 代码库中实现该解决方案时遇到了困难。
背景:
问题:文档中提供的代码片段如下:
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
LicenseDetailsCollectionResponse result = graphClient.users().byUserId("{user-id}").licenseDetails().get();
我尝试将此代码集成到我的 Java 应用程序中。虽然我可以使用 Postman 等工具成功查询 Microsoft Graph API,但我无法在我的 Java 代码库中复制该功能。
期望:我正在寻求帮助以理解和解决以下问题:
如何正确配置在 Java 中初始化
requestAdapter
实例所需的 GraphServiceClient
?
为了使 Microsoft Graph Java SDK 在 Java 应用程序中无缝工作,是否需要任何其他配置或依赖项?
对于如何解决此问题以及如何在我的 Java 应用程序中使用 Microsoft Graph Java SDK 成功检索用户的许可证详细信息,我表示感谢。
当我在 Graph Explorer 中运行 HTTP 调用时,我成功收到了包含许可证详细信息列表的响应,如下所示:
GET https://graph.microsoft.com/v1.0/users/userId/licenseDetails
回复:
要使用 Microsoft Graph Java SDK 获得相同的响应,您可以使用以下示例代码:
GraphAPIClient.java:
import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.models.LicenseDetails;
import com.microsoft.graph.models.ServicePlanInfo;
import com.microsoft.graph.requests.GraphServiceClient;
import com.microsoft.graph.requests.LicenseDetailsCollectionPage;
import java.util.Arrays;
import java.util.List;
public class GraphAPIClient {
// Azure AD authentication credentials
final String clientId = "appId";
final String tenantId = "tenantId";
final String clientSecret = "secret";
final List<String> scopes = Arrays.asList("https://graph.microsoft.com/.default");
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
public static void main(String[] args) {
GraphAPIClient graphAPIClient = new GraphAPIClient();
graphAPIClient.fetchAndListLicenseDetails();
}
public void fetchAndListLicenseDetails() {
// Authenticate with Azure AD
ClientSecretCredential credential = new ClientSecretCredentialBuilder()
.clientId(clientId).tenantId(tenantId).clientSecret(clientSecret)
.build();
TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(scopes, credential);
GraphServiceClient<GraphServiceClient> graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();
// Fetch and list license details of the user
try {
LicenseDetailsCollectionPage licenseDetailsCollection = graphClient.users("userId").licenseDetails().buildRequest().get();
while (licenseDetailsCollection != null) {
for (LicenseDetails licenseDetail : licenseDetailsCollection.getCurrentPage()) {
System.out.println("\nLicense ID: " + licenseDetail.id);
System.out.println("Service Plans:");
for (ServicePlanInfo servicePlanInfo : licenseDetail.servicePlans) {
System.out.println("\tService Plan ID: " + servicePlanInfo.servicePlanId);
System.out.println("\tService Plan Name: " + servicePlanInfo.servicePlanName);
}
System.out.println("----------------------------------");
// Convert object to JSON string with pretty printing
String json = gson.toJson(licenseDetail);
System.out.println("Complete JSON response:");
System.out.println(json);
}
if (licenseDetailsCollection.getNextPage() == null) {
break;
}
licenseDetailsCollection = licenseDetailsCollection.getNextPage().buildRequest().get();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
pom.xml
<dependencies>
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
回复: