问题描述

想通过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.javahttps://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

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

  1. 【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, ...

  2. 【Azure Developer】使用 CURL 获取 Key Vault 中 Secrets 中的值

    问题描述 在使用CURL通过REST API获取Azure Key Vaualt的Secrets值,提示Missing Token, 问如何来生成正确的Token呢? # curl 命令 curl - ...

  3. Ceph Object Gateway Admin api 获取用户列表问题

    按照官方文档使用Admin Ops API 获取用户列表 GET /admin/user时 返回{code: 403, message: Forbidden}这里有两个问题:首先用户列表的请求为 如下 ...

  4. php 实现微信模拟登陆、获取用户列表及群发消息功能示例

    本文实例讲述了php实现微信模拟登陆.获取用户列表及群发消息功能.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  5. 微信获取用户列表的json字符串解析

    今天学习微信遇到一个json的解析,但是因为自己的模型思维和思考能力很差一直困扰最后经过询问解决的问题,其实问题很简单总结起来就是json的解析: 注释:今天主要是讲怎样解析json的所以其他方法就只 ...

  6. 使用ShareSDK实现第三方授权登录、分享以及获取用户资料效果,项目中包含:源码+效果图+项目结构图

    [Android应用开发详解]第01期:第三方授权认证(一)实现第三方授权登录.分享以及获取用户资料   由于公司项目的需要,要实现在项目中使用第三方授权登录以及分享文字和图片等这样的效果,几经波折, ...

  7. 微信开放接口获取用户昵称保存到MySQL中为空白

    微信昵称中包含emoji表情标签,某些标签是使用了4字节编码的UTF8. 而大多数MySQL数据库现在使用的是3字节UTF8编码,这样会导致保存为空,且不会提示失败. 解决方法有2个,一个是升级到My ...

  8. 【Azure Developer】使用Microsoft Graph API 如何批量创建用户,用户属性中需要包含自定义字段(如:Store_code,Store_name等)

    Microsoft Graph 是 Microsoft 365 中通往数据和智能的网关. 它提供统一的可编程模型,可用于访问 Microsoft 365.Windows 10 和企业移动性 + 安全性 ...

  9. 【Azure Developer】使用Microsoft Graph API 批量创建用户,先后遇见的三个错误及解决办法

    问题描述 在先前的一篇博文中,介绍了如何使用Microsoft Graph API来创建Azure AD用户(博文参考:[Azure Developer]使用Microsoft Graph API 如 ...

随机推荐

  1. Flutter & App

    Flutter & App Android & iOS https://flutter.dev/docs/deployment/flavors https://flutter.dev/ ...

  2. Learn-JavaScript-with-MDN 系列文章: 01. var & let & const 对比

    Learn-JavaScript-with-MDN 系列文章: 01. var & let & const 对比 var & let & const 区别 https: ...

  3. C++算法代码——Sumsets[uva10125]

    题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1278 题目描述 给你一个整数的集合S(里面所有的整数均不相同),请你找出最大的 d, ...

  4. display: inline、block、inline-block、flex和inline-flex

    inline 共享一行 不能修改width.height属性,大小由内容撑开 padding属性 top.right.bottom.left设置都有效:margin属性只有left.right设置有效 ...

  5. Java自学第8期——多线程

    1.多线程: 操作系统支持同时运行多个任务,一个任务通常是一个程序,所有运行中的程序就是一个进程().程序内部包含多个顺序执行流,每个顺序执行流就是一个线程. 并发:两个或者多个事件在同一个时间段内交 ...

  6. 开发工具-scala处理json格式利器-json4s

    1.为什么是json4s 从json4s的官方描述 At this moment there are at least 6 json libraries for scala, not counting ...

  7. sql注入和union all关联查询的学习总结

    1.后台从页面取值进行sql查询时最好不要直接拼,如下代码: String sql = "SELECT wo.* " + " from push_command pu & ...

  8. oracle check datapump jobs

    reference: https://asktom.oracle.com/pls/apex/asktom.search?tag=getting-ora-31626-job-does-not-exist ...

  9. 后端程序员之路 51、A Tour of Go-1

    # A Tour of Go    - go get golang.org/x/tour/gotour    - https://tour.golang.org/    # welcome    - ...

  10. Oracle VM VirtualBox下创建CentOS虚拟系统

    下载镜像 创建虚拟电脑 点击新建,输入服务器命名(根据自己喜好),选择好类型和版本(我下载的是64位的CentOS系统,所以选择类型为Linux,版本为其他版本). 修改内存大小 系统建议为512M, ...