var address = new java.net.InetSocketAddress("proxy.example", 8080);
var tokenProxy = new com.azure.core.http.ProxyOptions(
    com.azure.core.http.ProxyOptions.Type.HTTP, address);
var tokenHttp = new com.azure.core.http.netty.NettyAsyncHttpClientBuilder()
    .proxy(tokenProxy).build();
var credential = new com.azure.identity.ClientSecretCredentialBuilder()
    .tenantId(tenantId).clientId(clientId).clientSecret(clientSecret)
    .httpClient(tokenHttp).build();

String[] scopes = {"https://graph.microsoft.com/.default"};
String[] allowedHosts = {"graph.microsoft.com"};
var auth = new com.microsoft.kiota.authentication.AzureIdentityAuthenticationProvider(
    credential, allowedHosts, scopes);
var graphHttp = com.microsoft.graph.core.requests.GraphClientFactory.create()
    .proxy(new java.net.Proxy(java.net.Proxy.Type.HTTP, address))
    .build();
var graph = new com.microsoft.graph.serviceclient.GraphServiceClient(auth, graphHttp);

沿用现有的租户/客户端凭据。当令牌与 Graph 端点都允许使用公共中转时,HTTP 代理列表可为共享的无账号 HTTP 代理地址提供候选。所示的公有云范围与允许的主机必须与预期的云部署一致。

httpClient(tokenHttp) 提供 Azure Identity 的令牌路由。GraphClientFactory.create() 在添加 API 代理的同时保留 Graph 的默认中间件;各项操作请使用最终的 graph 客户端。

需要认证的中转要求在两条路径上都配置账号:Azure 的 ProxyOptions.setCredentials 与 OkHttp 的 proxyAuthenticator 分别服务各自的传输层。缓存的令牌可能掩盖尚未配置的令牌路径。

自有传输应随该组件一并保留并清理。较旧的 TokenCredentialAuthProvider 构建器面向另一代 SDK。

相关文章