使用 Java 将代理设置添加到 Microsoft Graph 客户端

hor*_*zon 2 java proxy azure-ad-graph-api azure-ad-b2c microsoft-graph-api

我正在尝试实现 GraphClient,下面是一个运行良好的示例代码...

   ClientCredentialProvider authProvider = 
              new ClientCredentialProvider(clientId,
                      scopes,
                      clientSecret,
                      b2cTenant,
                      endpoint);
    
    IGraphServiceClient graphClient = graphClient = GraphServiceClient.builder()
                .authenticationProvider(authProvider)
                .buildClient();
Run Code Online (Sandbox Code Playgroud)

这工作正常......但在某些情况下,从运行代码的地方,有一个代理,所以我需要设置代理来连接到互联网。我需要设置代理并将其传递给 graphClient 以某种方式告诉通过代理进行调用。

我试图找到一份文件,但找不到任何文件,我得到了这个......

ProxyOptions proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress(proxyUrl, proxyPort));
proxyOptions.setCredentials(proxyUser, proxyPassword);

final UsernamePasswordCredential usernamePasswordCredential = new UsernamePasswordCredentialBuilder()
                    .clientId(clientId)
                    .username(username)
                    .password(password)
                    .httpClient(HttpClient.createDefault(new HttpClientOptions().setProxyOptions(proxyOptions)))
                    .build();
Run Code Online (Sandbox Code Playgroud)

但问题是“ProxyOptions”不在 Maven 中,我不确定它属于哪个库。

任何人都可以提出一个想法。

Sha*_*tta 5

更新答案...

        ProxyOptions proxyOptions = new ProxyOptions(
               ProxyOptions.Type.HTTP, new InetSocketAddress(hostAddress, hostPort));
        
        HttpClientOptions clientOptions = new HttpClientOptions();
        clientOptions.setProxyOptions(proxyOptions); 
        
        HttpClient azHttpClient = HttpClient.createDefault(clientOptions);
        
        ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                .clientId(clientId)
                .clientSecret(clientSecret)
                .tenantId(tenantId)
                .httpClient(azHttpClient)
                .build();

        TokenCredentialAuthProvider tokenCredentialAuthProvider = 
            new TokenCredentialAuthProvider(scopes, clientSecretCredential);
        
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostAddress, hostPort));
        
        OkHttpClient httpClient = HttpClients.createDefault(tokenCredentialAuthProvider)
                .newBuilder()
                .proxy(proxy)
                .build();

        graphClient = GraphServiceClient.builder()
                .authenticationProvider(tokenCredentialAuthProvider)
                .httpClient(httpClient)
                .buildClient();
Run Code Online (Sandbox Code Playgroud)