gpt4 book ai didi

java - 如何在java中获取客户端 key 到期日期

转载 作者:行者123 更新时间:2023-12-02 01:30:20 26 4
gpt4 key购买 nike

我在 Azure AD 租户中注册了应用程序,这些应用程序具有 clientid 和 key 。我需要获取 azure 应用程序凭据的到期日期。我在我的应用程序中使用azure sdk for java。我们如何使用 java 获取客户端 secret 的到期日期?

我用谷歌搜索过,但没有找到任何有用的链接。谁能帮我解决这个问题。

最佳答案

如果您想在 Java 应用程序中获取 secret 到期日期,您可以调用 Microsoft Graph API获取应用程序。然后应用程序的属性passwordCredentials有信息。例如

使用 Azure 门户注册新应用程序

  1. 使用工作或学校帐户登录 Azure 门户,或者个人 Microsoft 帐户。

  2. 如果您的帐户允许您访问多个租户,请在右上角选择您的帐户,然后将您的门户 session 设置为所需的 Azure AD 租户。

  3. 在左侧导航 Pane 中,选择 Azure Active Directory 服务,然后选择应用注册 > 新注册。

配置应用程序所需的 Microsoft Graph 权限 enter image description here

代码

  //install ADAL4J get accesss token
String clientId = "your application id";
String appKey = "your client secret";
String tenantId = "your tenant id";
String authority =String.format("https://login.microsoftonline.com/",getTenantContextId())
String resourceUrl = "https://graph.microsoft.com"
ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationContext context = ew AuthenticationContext(authority, false, service);
ClientCredential clientCred = new ClientCredential(
clientId, appKey);
Future<AuthenticationResult> future = context.acquireToken(resourceUrl, clientCred, null);
AuthenticationResult result = future.get();

//Call Microsoft graph api
String stringUrl ="https://graph.microsoft.com/beta/applications?$filter=appId eq '{ApplicationId}'";
URL url = new URL(stringUrl.replaceAll(" ","%20"));

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Bearer " + result.getAccessToken());
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");

int httpResponseCode = conn.getResponseCode();
if (httpResponseCode == 200 ) {
BufferedReader in = null;
StringBuilder response;

in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}

in.close();
JSONObject jb = new JSONObject(response.toString());


更多详情请引用document

更新

请使用以下代码获取访问 token

String authority = "https://login.microsoftonline.com/" + tenant;
ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationContext context = new AuthenticationContext(authority, true, service);
ClientCredential cred = new ClientCredential(clientId, clientSecret);
String resourceId ="https://graph.microsoft.com";
Future<AuthenticationResult> future = context.acquireToken(resourceId, cred, null);
AuthenticationResult result = future.get();
String accesstoken = result.getAccessToken();

enter image description here

关于java - 如何在java中获取客户端 key 到期日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57556944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com