在访问 Graph 的应用程序中发生了令牌过期的事件 API

An event has occurred in which the token expires in the application that accesses the Graph API

发生了访问图形的应用程序中令牌过期的事件API。

将令牌到期日期从默认的 60 分钟增加, 我正在寻找 Java 源代码以在令牌过期时更新令牌。

我在下面的 post 中找到了示例代码的 link,但是 link 被破坏了。

将令牌到期日期从默认的 60 分钟增加, 是否有一些 Java 示例代码可以在令牌过期时更新令牌?

您可以使用 Create tokenLifetimePolicy 图 API。

POST https://graph.microsoft.com/v1.0/policies/tokenLifetimePolicies
Content-type: application/json

{
  "definition": [
    "{\"TokenLifetimePolicy\":{\"Version\":1,\"AccessTokenLifetime\":\"8:00:00\"}}"
  ],
  "displayName": "CustomTokenLifetimePolicy",
  "isOrganizationDefault": true
}

在java中:

注意:先添加需要的应用权限。

// ClientSecretCredential needs application permission with client credential flow.
final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
        .clientId(CLIENT_ID)
        .clientSecret(CLIENT_SECRET)
        .tenantId(TENANT_GUID)
        .build();

final TokenCredentialAuthProvider tokenCredAuthProvider = new TokenCredentialAuthProvider(SCOPES, clientSecretCredential);

final GraphServiceClient graphClient = GraphServiceClient
                .builder()
                .authenticationProvider(tokenCredAuthProvider)
                .buildClient();

TokenLifetimePolicy tokenLifetimePolicy = new TokenLifetimePolicy();
LinkedList<String> definitionList = new LinkedList<String>();
definitionList.add("definition-value");
tokenLifetimePolicy.definition = definitionList;
tokenLifetimePolicy.displayName = "displayName-value";
tokenLifetimePolicy.isOrganizationDefault = true;

graphClient.policies().tokenLifetimePolicies()
    .buildRequest()
    .post(tokenLifetimePolicy);