【Azure Developer】示例: 在中国区调用MSGraph SDK通过User principal name获取到User信息,如Object ID

简介: 【Azure Developer】示例: 在中国区调用MSGraph SDK通过User principal name获取到User信息,如Object ID

问题描述

示例调用MSGraph SDK通过User principal name获取到User信息,如Object ID。

 

参考资料

选择 Microsoft Graph 身份验证提供程序 : https://learn.microsoft.com/zh-cn/graph/sdks/choose-authentication-providers?tabs=java#using-a-client-secret-2

Microsoft Graph SDK for Java : https://github.com/microsoftgraph/msgraph-sdk-java

Azure China developer guide : https://learn.microsoft.com/en-us/azure/china/resources-developer-guide#check-endpoints-in-azure

 

示例代码

第一步:在POM.XML中添加对 com.microsoft.graph 的依赖

<dependency>
  <!-- Include the sdk as a dependency -->
  <groupId>com.microsoft.graph</groupId>
  <artifactId>microsoft-graph</artifactId>
  <version>5.73.0</version>
</dependency>

第二步:引用代码

String clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
        String clientSecret = "application secret";
        String tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
        // The client credentials flow requires that you request the
        // /.default scope, and pre-configure your permissions on the
        // app registration in Azure. An administrator must grant consent
        // to those permissions beforehand.
        java.util.List<String> scopes = Arrays.asList("https://microsoftgraph.chinacloudapi.cn/.default");
        ClientSecretCredential credential = new ClientSecretCredentialBuilder()
                .authorityHost("https://login.partner.microsoftonline.cn")
                .clientId(clientId).tenantId(tenantId).clientSecret(clientSecret).build();
        if (null == scopes || null == credential) {
            throw new Exception("Unexpected error");
        }
        
        TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(
                scopes, credential);
        GraphServiceClient<okhttp3.Request> graphClient = GraphServiceClient.builder()
                .authenticationProvider(authProvider).buildClient();
        // Specify the user principal name
        String userPrincipalName = "user principal name";
        graphClient.setServiceRoot("https://microsoftgraph.chinacloudapi.cn/v1.0");
        // Use the GraphServiceClient to get the user by user principal name
        User user = graphClient.users(userPrincipalName)
                .buildRequest()
                .get();
        // Get the user object ID
        String objectId = user.id;

注意事项

1)因为这是在中国区Azure,所以AAD认证,Graph Endpoint都想要切换到中国Azure环境

2) 如果遇见403 FORBIDDEN的情况,则想要为代码中所使用的AAD注册应用添加Microsoft.Graph的User.read.all权限

结果展示

相关文章
|
存储 Java API
【Azure 存储服务】Java Storage SDK 调用 uploadWithResponse 代码示例(询问ChatGTP得代码原型后人力验证)
【Azure 存储服务】Java Storage SDK 调用 uploadWithResponse 代码示例(询问ChatGTP得代码原型后人力验证)
183 0
|
Kubernetes API 开发工具
【Azure Developer】通过SDK(for python)获取Azure服务生命周期信息
需要通过Python SDK获取Azure服务的一些通知信息,如:K8S版本需要更新到指定的版本,Azure服务的维护通知,服务处于不健康状态时的通知,及相关的操作建议等内容。
232 18
|
机器学习/深度学习 开发工具 Python
【Azure 应用服务】使用Python Azure SDK 来获取 App Service的访问限制信息(Access Restrictions)
【Azure 应用服务】使用Python Azure SDK 来获取 App Service的访问限制信息(Access Restrictions)
179 0
|
JSON Java API
【Azure API 管理】通过Java APIM SDK创建一个新的API,如何为Reqeust的Representation设置一个内容示例(Sample)?
【Azure API 管理】通过Java APIM SDK创建一个新的API,如何为Reqeust的Representation设置一个内容示例(Sample)?
143 0
|
存储 API 开发工具
【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例
【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例
198 0
|
Java 开发工具 Windows
【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例
【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例
143 0
|
11月前
|
安全 IDE Java
重学Java基础篇—Java Object类常用方法深度解析
Java中,Object类作为所有类的超类,提供了多个核心方法以支持对象的基本行为。其中,`toString()`用于对象的字符串表示,重写时应包含关键信息;`equals()`与`hashCode()`需成对重写,确保对象等价判断的一致性;`getClass()`用于运行时类型识别;`clone()`实现对象复制,需区分浅拷贝与深拷贝;`wait()/notify()`支持线程协作。此外,`finalize()`已过时,建议使用更安全的资源管理方式。合理运用这些方法,并遵循最佳实践,可提升代码质量与健壮性。
346 1
|
11月前
|
Java
课时78:Object类的基本概念
Object类的主要特点是可以解决参数的统一问题,使用object类可以接受所有的数据类型。 1. Object类简介 2. 观察Object类接收所有子类对象 3. 使用Object类接收数组
217 0
|
JSON Java Apache
Java基础-常用API-Object类
继承是面向对象编程的重要特性,允许从已有类派生新类。Java采用单继承机制,默认所有类继承自Object类。Object类提供了多个常用方法,如`clone()`用于复制对象,`equals()`判断对象是否相等,`hashCode()`计算哈希码,`toString()`返回对象的字符串表示,`wait()`、`notify()`和`notifyAll()`用于线程同步,`finalize()`在对象被垃圾回收时调用。掌握这些方法有助于更好地理解和使用Java中的对象行为。
189 8
|
存储 Java 程序员
Java基础的灵魂——Object类方法详解(社招面试不踩坑)
本文介绍了Java中`Object`类的几个重要方法,包括`toString`、`equals`、`hashCode`、`finalize`、`clone`、`getClass`、`notify`和`wait`。这些方法是面试中的常考点,掌握它们有助于理解Java对象的行为和实现多线程编程。作者通过具体示例和应用场景,详细解析了每个方法的作用和重写技巧,帮助读者更好地应对面试和技术开发。
549 4

热门文章

最新文章