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

azure,developer,示例,中国区,调用,msgraph,sdk,通过,user,principal,name,获取,信息,object,id · 浏览次数 : 4

小编点评

**依赖** ```xml com.microsoft.graph microsoft-graph 5.73.0 ``` **代码** ```java // Client credentials flow parameters String clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; String clientSecret = "application secret"; String tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Define the scopes for token acquisition List scopes = Arrays.asList("https://microsoftgraph.chinacloudapi.cn/.default"); // Create a token credentials provider ClientSecretCredential credential = new ClientSecretCredentialBuilder() .authorityHost("https://login.partner.microsoftonline.cn") .clientId(clientId) .tenantId(tenantId) .clientSecret(clientSecret) .build(); // Create a GraphServiceClient with authentication provider GraphServiceClient&graphClient = GraphServiceClient.builder() .authenticationProvider(authProvider) .buildClient(); // Set the service root to the Microsoft Graph API v1.0 graphClient.setServiceRoot("https://microsoftgraph.chinacloudapi.cn/v1.0"); // Get the user by user principal name String userPrincipalName = "user principal name"; // Get the user object ID String objectId = user.id; // Display the user object ID System.out.println("Object ID: " + objectId); ``` **注意** * 该代码需要在您的应用程序中配置Microsoft Graph API凭证。 * 您需要为您的应用程序添加Microsoft Graph的User.read.all权限才能获取用户对象 ID。 * 如果您遇到403 FORBIDDEN错误,请确保您在 Azure环境中设置正确的凭证。

正文

问题描述

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

结果展示

 

 

[END]

与【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-

【Azure Developer】Go语言调用Azure SDK如何登录到中国区Azure环境

问题描述 在 “使用 Azure SDK for Go 进行 Azure 身份验证” 文章中的 Go 示例代码进行登录Azure时,默认指向的是Globa Azure。当只修改AAD AZURE_CLIENT_ID , AZURE_TENANT_ID 和 AZURE_CLIENT_SECRET参数值

【Azure Developer】使用 Microsoft Graph API 获取 AAD User 操作示例

问题描述 查看官方文档“ Get a user ” , 产生了一个操作示例的想法,在中国区Azure环境中,演示如何获取AAD User信息。 问题解答 使用Microsoft Graph API,演示如何获取AAD User信息,因参考文档是针对Global Azure,所以文档种的URL为: /

【Azure Developer】在App Service上放置一个JS页面并引用msal.min.js成功获取AAD用户名示例

问题描述 在App Service上放置一个JS页面并引用msal.min.js,目的是获取AAD用户名并展示。 问题解答 示例代码 Azure Service

【Azure Developer】.Net 简单示例 "文字动图显示" Typing to SVG

问题描述 看见一个有趣的页面,可以把输入的文字信息,直接输出SVG图片,还可以实现动图模式。 示例URL: https://readme-typing-svg.demolab.com/?font=Fira+Code&pause=1000&color=F7F7F7&background=233911F

【Azure Developer】如何通过Azure Portal快速获取到对应操作的API并转换为Python代码

问题描述 对于Azure资源进行配置操作,门户上可以正常操作。但是想通过Python代码实现,这样可以批量处理。那么在没有SDK的情况下,是否有快速办法呢? 问题解答 当然可以,Azure Portal上操作的所有资源都是通过REST API来实现的,所以只要找到正确的API,就可以通过浏览器中抓取

【Azure Developer】use @azure/arm-monitor sdk 遇见 ManagedIdentityCredential authentication failed.(status code 500)

@azure/arm-monitor ManagedIdentityCredential authentication failed.(status code 500) CredentialUnavailableError: ERROR: AADSTS500011: The resource principal name https://management.azure.com was not

【Azure Developer】Springboot 集成 中国区的Key Vault 报错 AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found

spring.cloud.azure.keyvault.secret.property-sources[0].profile.cloud-type=AZURE_CHINA

【Azure Developer】开发模式下使用AAD账号访问Azure Blob的相关参考

问题描述 开发模式下使用AAD账号访问Azure Blob的流程参考文件 问题解答 第一步:先在AAD中注册一个APP,步骤可参考: 将应用程序注册到 Microsoft 标识平台 :https://docs.azure.cn/zh-cn/active-directory/develop/quick

【Azure Developer】Github Action使用Azure/login@v1插件登录遇见错误的替代方案

问题描述 在使用 Github Action - Azure/login@v1 的插件时候,登录中国区Azure遇见了问题。 Login YAML 内容: - name: 'Login via Azure CLI' uses: Azure/login@v1.4.6 with: creds: ${{