java - 如何在发布映射 URL 中传递tenant_id
问题描述
在邮递员中有以下 URL 以获取访问令牌。
https://login.microsoftonline.com/:tenant_id/oauth2/token
现在编写java代码来做我在邮递员中做的同样的事情。
这是示例代码
公共类请求测试{
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
String tenant_id = "<tenant_id>";
String keys = "<client_id>:<client_secret>";
String url = "https://login.microsoftonline.com/:" + tenant_id + "/oauth2/token";
HashMap<String, String> parameters = new HashMap<>();
parameters.put("grant_type", "client_credentials");
String form = parameters.keySet().stream().map(key -> key + "=" + URLEncoder.encode(parameters.get(key),StandardCharsets.UTF_8)).collect(Collectors.joining("&"));
String encoding = Base64.getEncoder().encodeToString(keys.getBytes());
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url))
.headers("Content-Type", "application/x-www-form-urlencoded", "Authorization", "Basic "+encoding)
.POST(BodyPublishers.ofString(form)).build();
HttpResponse<?> response = client.send(request, BodyHandlers.ofString());
System.out.println(response.statusCode() + response.body().toString());
}
}
但不知何故,URL 没有正确形成,因为我看到以下错误:
400{"error":"invalid_request","error_description":"AADSTS900023: 指定的租户标识符 ':tenant_id' 既不是有效的 DNS 名称,也不是有效的外部域。\r\n跟踪 ID: 652e1996-1863-4183-aac5 -ee9a74680600\r\n相关 ID: 45396fd8-ee9c-423b-ae5d-3bf8885d4532\r\n时间戳: 2021-04-15 09:24:51Z","error_codes":[900023],"timestamp":"2021-04 -15 09:24:51Z","trace_id":"652e1996-1863-4183-aac5-ee9a74680600","correlation_id":"45396fd8-ee9c-423b-ae5d-3bf8885d4532","error_uri":"https:// login.microsoftonline.com/error?code=900023"}
请建议我在代码中缺少的地方?请提供任何参考资料。谢谢
解决方案
请求url错误,去掉就好:
了,应该是
https://login.microsoftonline.com/" + tenant_id + "/oauth2/token
推荐阅读
- dev-c++ - 创建一个猜谜游戏直到我想停下来
- javascript - 将显示在HTML 表单中的结果传递给另一个页面
- java - 使用 Rhino 将动态构造的对象作为参数传递给 JS 函数
- c# - C#想知道是否有任何枚举类型仅在限制中重复其值
- r - 减少 R 中移动内核的计算时间
- c# - 在 C# 中序列化枚举时如何解决程序集 ID 错误?
- regex - 更改 oracle 正则表达式以接受带有连字符“-”的电子邮件域
- r - R中未使用的参数错误:漩涡函数模块
- ios - 如何修复:tvOS xibs 不支持目标设备类型的 homepod?
- javascript - 如何在JS中执行逐位乘法