【Azure Developer】Azure Graph SDK获取用户列表的问题: SDK中GraphServiceClient如何指向中国区的Endpoint:https://microsoftgraph.chinacloudapi.cn/v1.0

简介: 【Azure Developer】Azure Graph SDK获取用户列表的问题: SDK中GraphServiceClient如何指向中国区的Endpoint:https://microsoftgraph.chinacloudapi.cn/v1.0

问题描述

想通过Java SDK的方式来获取Azure 门户中所列举的用户。一直报错无法正常调用接口,错误信息与AAD登录认证相关,提示tenant not found。

想要实现的目的,通过代码方式获取如下User List(https://portal.azure.cn/#blade/Microsoft_AAD_IAM/UsersManagementMenuBlade/MsGraphUsers

JAVA 代码

错误截图

如何来解决获取AAD认证的问题呢?

 

解决方法

在代码中,已经设置了AAD登录Scopes 为China Azure (https://microsoftgraph.chinacloudapi.cn/.default)。 但是GraphServiceClient对象依旧导向到Global的Endpoint,在查看GraphServiceClient的源码发现它为固定值("https://graph.microsoft.com/v1.0")。而该类没有提供可以重写该参数的方法,导致在最终请求时,每次生成的GraphServiceClient对象都无法请求到china的Endpoint。

 

这也就导致了即使输入正确China AAD认证信息但依旧无法登录成功。最后,找到了一个Graph扩展类中的IGraphServiceClient类,它提供了setServiceRoot的方法,需要引用import com.microsoft.graph.models.extensions.IGraphServiceClient;

然后在代码中修改GraphServiceClient定义(代码中高亮部分

package GraphTest;
import com.microsoft.graph.auth.confidentialClient.ClientCredentialProvider;
import com.microsoft.graph.auth.enums.NationalCloud;
import com.microsoft.graph.models.extensions.IGraphServiceClient;
import com.microsoft.graph.requests.extensions.GraphServiceClient;
import java.util.ArrayList;
public class TestBase_Customer_Solve {
    private String clientId="";
    private String clientSecret="";
    private String grantType = "client_credentials";
    private String tokenEndpoint = "https://login.partner.microsoftonline.cn/{teantId}/oauth2/v2.0/token";
    private String resourceId = "https://microsoftgraph.chinacloudapi.cn/.default";
    private String teantId = "";
    public IGraphServiceClient graphClient = null;
    public IGraphServiceClient GetClient(boolean authenticate)
    {
        if (graphClient == null) {
            try {
                ArrayList<String> scope = new ArrayList();
                scope.add( resourceId );
                ClientCredentialProvider authProvider = new ClientCredentialProvider(
                        clientId,
                        scope,
                        clientSecret,
                        teantId,
                        NationalCloud.China);
                 graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
                 graphClient.setServiceRoot( "https://microsoftgraph.chinacloudapi.cn/v1.0" );
                return graphClient;
            }
            catch (Exception e)
            {
                throw new Error("Could not create a graph client: " + e.getLocalizedMessage());
            }
        }
        return null;
    } 
    
}

在修改了Graph Client的Service Root为https://microsoftgraph.chinacloudapi.cn/v1. 最终是成功拿到了Users的列表数据。

 

参考资料

msgraph-sdk-java-authhttps://github.com/microsoftgraph/msgraph-sdk-java-auth 

GraphServiceClient.java: https://github.com/microsoftgraph/msgraph-sdk-java/blob/4638206053a5545a6d6ba73168337939cde34fed/src/main/java/com/microsoft/graph/requests/GraphServiceClient.java

Get a GraphServiceClient objecthttps://github.com/microsoftgraph/msgraph-sdk-java#23-get-a-graphserviceclient-object

 

相关文章
|
24天前
|
JavaScript 前端开发 API
【Azure Developer】use @azure/arm-monitor sdk 遇见 ManagedIdentityCredential authentication failed.(status code 500)
【Azure Developer】use @azure/arm-monitor sdk 遇见 ManagedIdentityCredential authentication failed.(status code 500)
|
1天前
|
Kubernetes API 开发工具
【Azure Developer】通过SDK(for python)获取Azure服务生命周期信息
需要通过Python SDK获取Azure服务的一些通知信息,如:K8S版本需要更新到指定的版本,Azure服务的维护通知,服务处于不健康状态时的通知,及相关的操作建议等内容。
34 18
|
24天前
|
存储 API 开发工具
【Azure Storage Blob】如何通过.NET Azure Storage Blobs SDK获取到Blob的MD5值呢?
【Azure Storage Blob】如何通过.NET Azure Storage Blobs SDK获取到Blob的MD5值呢?
|
24天前
|
Java 开发工具
【Azure Developer】示例: 在中国区调用MSGraph SDK通过User principal name获取到User信息,如Object ID
【Azure Developer】示例: 在中国区调用MSGraph SDK通过User principal name获取到User信息,如Object ID
|
24天前
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
|
24天前
|
安全 网络安全 Windows
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
|
24天前
|
开发工具 iOS开发 容器
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
|
24天前
|
消息中间件 开发工具
【Azure Service Bus】Service Bus SDK 抛出 ERROR c.a.c.a.i.ActiveClientTokenManager - Error is transient. Rescheduling authorization task at interval 1079000 ms.
【Azure Service Bus】Service Bus SDK 抛出 ERROR c.a.c.a.i.ActiveClientTokenManager - Error is transient. Rescheduling authorization task at interval 1079000 ms.
|
24天前
|
开发工具
【Azure Event Hub】解决Event Hub SDK出现无法识别 com.azure.core.client.traits.TokenCredentialTrait 错误
【Azure Event Hub】解决Event Hub SDK出现无法识别 com.azure.core.client.traits.TokenCredentialTrait 错误
|
3月前
|
安全 网络协议 网络安全
IP代理的三大协议:HTTP、HTTPS与SOCKS5的区别
**HTTP代理**适用于基本网页浏览,简单但不安全;**HTTPS代理**提供加密,适合保护隐私;**SOCKS5代理**灵活强大,支持TCP/UDP及认证,适用于绕过限制。选择代理协议应考虑安全、效率及匿名需求。

热门文章

最新文章