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

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

时间:2023-10-10 21:32:39浏览次数:36  
标签:xxxx cn 示例 name user https microsoft User

问题描述

示例调用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

Microsoft Graph

https://graph.microsoft.com

https://microsoftgraph.chinacloudapi.cn

示例代码

第一步:在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环境

  • AAD Login Endpoint: https://login.partner.microsoftonline.cn
  • Ms Graph: https://microsoftgraph.chinacloudapi.cn/v1.0

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

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

结果展示

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

 

 

[END]


当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

标签:xxxx,cn,示例,name,user,https,microsoft,User
From: https://blog.51cto.com/u_13773780/7799629

相关文章

  • 移除ID名为id_name的元素和id置空
    1、移除ID名为id_name的元素varx=document.getElementById("id_name");//如果对象x不为空if(x!=null)x.remove();对于selectObject.remove(index)方法,以select元素为例,若指定index,则只删除其中的某一项;若不指定index,则删除整个select元素。2、移除Class名为class_name......
  • 强制等待和隐式等待示例
    背景知识 发送回车键  fromselenium.webdriver.common.keysimportKeysdriver.find_element(By.CSS_SELECTOR,loc).send_keys(Keys.ENTER)  隐式等待的意思是当符合条件就结束等待,不符合持续等待对比   第一种 强制等待fromseleniumimportwebdri......
  • django 设置外键的时候,related_name的值大写还是小写,规则怎样
    django设置外键的时候,related_name的值大写还是小写,规则怎样在Django中,related_name参数用于定义反向关系的名称,即通过外键字段反向查询关联模型的对象。related_name的值是一个字符串,可以是大写也可以是小写,但通常建议使用小写字母,因为它们更符合Python的命名约定(PEP8)。具体......
  • Composite 组合模式简介与 C# 示例【结构型3】【设计模式来了_8】
    〇、简介1、什么是组合设计模式?一句话解释:  针对树形结构的任意节点,都实现了同一接口,他们具有相同的操作,可以通过某一操作来遍历全部节点。组合模式通过使用树形结构来组合对象,用来表示部分以及整体层次。组合模式属于结构型模式,多用于递归。官方意图描述:将对象组合成树形......
  • navigator.userAgent 判断当前浏览器所处的环境
    场景1:判断页面是在手机端还是PC端打开window.location.href=/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)?"http://localhost:8888/mobile_web":"http://localhost:8888/PC";场景2:判断页面是在手机端,平板端还是PC端打开varos=function(){va......
  • 常用Stream API示例
    1.flatMap    将多个集合压平为一个集合List<Long>siteIdList=routeLines.stream() .flatMap(routeLine->Stream.of(routeLine.getSubLineFSiteId(),routeLine.getSubLineTSiteId())) .distinct() .collect(Collectors.toList());2.Collectors.toMap()......
  • drf(序列化练习、user表练习)
    一.APIView版本1.models.pyfromdjango.dbimportmodels#Createyourmodelshere.classCommonField(models.Model):is_delete=models.BooleanField(default=0,verbose_name='True标记被删除的数据,False标记正常使用的数据')create_time=models.DateT......
  • MYSQL-sql语句示例
    数据库操作:创建数据库:CREATEDATABASEdatabase_nameCHARACTERSETutf8mb4COLLATEutf8mb4_unicode_ci;删除数据库:DROPDATABASEIFEXISTSdatabase_name;切换数据库:USEdatabase_name;表的管理:创建表:CREATETABLEtable_name(column1datatypeconstraint,colu......
  • Bridge 桥接模式简介与 C# 示例【结构型2】【设计模式来了_7】
    〇、简介1、什么是桥接模式?一句话解释:  通过一个类的抽象,与另一个类的抽象关联起来,当做桥。此后不管两个抽象类的实现有多少种,均可以通过这个桥来将两个对象联系起来。桥接,顾名思义就是用桥来连接河两岸,将原本不关联的两部分联系起来,且不影响两岸的各自演化,演化出来的不同对......
  • ModuleNotFoundError: No module named ‘xxx‘可能的解决方案大全
    "ModuleNotFoundError:Nomodulenamed'xxx'"这个报错是个非常常见的报错,几乎每个python程序员都遇到过,导致这个报错的原因也非常多,下面是我曾经遇到过的原因和解决方案module包没安装忘了import没有__init__.py文件package包的版本不对自定义的包名与安装的包名相同,导致i......