【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-auth:https://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 object:https://github.com/microsoftgraph/msgraph-sdk-java#23-get-a-graphserviceclient-object
【Azure Developer】Azure Graph SDK获取用户列表的问题: SDK中GraphServiceClient如何指向中国区的Endpoint:https://microsoftgraph.chinacloudapi.cn/v1.0的更多相关文章
- 【Azure Developer】使用Postman获取Azure AD中注册应用程序的授权Token,及为Azure REST API设置Authorization
Azure Active Directory (Azure AD) is Microsoft's cloud-based identity and access management service, ...
- 【Azure Developer】使用 CURL 获取 Key Vault 中 Secrets 中的值
问题描述 在使用CURL通过REST API获取Azure Key Vaualt的Secrets值,提示Missing Token, 问如何来生成正确的Token呢? # curl 命令 curl - ...
- Ceph Object Gateway Admin api 获取用户列表问题
按照官方文档使用Admin Ops API 获取用户列表 GET /admin/user时 返回{code: 403, message: Forbidden}这里有两个问题:首先用户列表的请求为 如下 ...
- php 实现微信模拟登陆、获取用户列表及群发消息功能示例
本文实例讲述了php实现微信模拟登陆.获取用户列表及群发消息功能.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- 微信获取用户列表的json字符串解析
今天学习微信遇到一个json的解析,但是因为自己的模型思维和思考能力很差一直困扰最后经过询问解决的问题,其实问题很简单总结起来就是json的解析: 注释:今天主要是讲怎样解析json的所以其他方法就只 ...
- 使用ShareSDK实现第三方授权登录、分享以及获取用户资料效果,项目中包含:源码+效果图+项目结构图
[Android应用开发详解]第01期:第三方授权认证(一)实现第三方授权登录.分享以及获取用户资料 由于公司项目的需要,要实现在项目中使用第三方授权登录以及分享文字和图片等这样的效果,几经波折, ...
- 微信开放接口获取用户昵称保存到MySQL中为空白
微信昵称中包含emoji表情标签,某些标签是使用了4字节编码的UTF8. 而大多数MySQL数据库现在使用的是3字节UTF8编码,这样会导致保存为空,且不会提示失败. 解决方法有2个,一个是升级到My ...
- 【Azure Developer】使用Microsoft Graph API 如何批量创建用户,用户属性中需要包含自定义字段(如:Store_code,Store_name等)
Microsoft Graph 是 Microsoft 365 中通往数据和智能的网关. 它提供统一的可编程模型,可用于访问 Microsoft 365.Windows 10 和企业移动性 + 安全性 ...
- 【Azure Developer】使用Microsoft Graph API 批量创建用户,先后遇见的三个错误及解决办法
问题描述 在先前的一篇博文中,介绍了如何使用Microsoft Graph API来创建Azure AD用户(博文参考:[Azure Developer]使用Microsoft Graph API 如 ...
随机推荐
- google advanced search operators
google advanced search operators https://www.google.com/advanced_search js es6 site:xgqfrms.xyz http ...
- react-parent-child-lifecycle-order
react-parent-child-lifecycle-order react parent child lifecycle order live demo https://33qrr.csb.ap ...
- 召回 & 召回算法
召回 & 召回算法 recall https://developers.google.com/machine-learning/crash-course/classification/prec ...
- css picture
css picture https://github.com/cyanharlow https://diana-adrianne.com/ demo https://github.com/cyanha ...
- js navigator.wakeLock 保持屏幕唤醒状态
let lock; btn.addEventListener("click", async () => { try { if (lock) { lock.release(); ...
- c++ 使用PID获取顶级窗口句柄和标题
#include <iostream> #include <Windows.h> using namespace std; BOOL CALLBACK EnumWindowsP ...
- Baidu Apollo use: command " rosbag " not fonud
https://github.com/ApolloAuto/apollo/issues/181 1.If using dev docker env, you need run apollo.sh bu ...
- 深入理解 Web 协议 (三):HTTP 2
本篇将详细介绍 HTTP 2 协议的方方面面,知识点如下: HTTP 2 连接的建立 HTTP 2 中帧和流的关系 HTTP 2 中流量节省的奥秘:HPACK 算法 HTTP 2 协议中 Server ...
- Java并发之ThreadPoolExecutor源码解析(二)
ThreadPoolExecutor ThreadPoolExecutor是ExecutorService的一种实现,可以用若干已经池化的线程执行被提交的任务.使用线程池可以帮助我们限定和整合程序资源 ...
- python进阶(11)生成器
生成器 利用迭代器,我们可以在每次迭代获取数据(通过next()方法)时按照特定的规律进行生成.但是我们在实现一个迭代器时,关于当前迭代到的状态需要我们自己记录,进而才能根据当前状态生成下一个数据. ...