我正在尝试实现 GraphClient,下面是一个运行良好的示例代码...
ClientCredentialProvider authProvider =
new ClientCredentialProvider(clientId,
scopes,
clientSecret,
b2cTenant,
endpoint);
IGraphServiceClient graphClient = graphClient = GraphServiceClient.builder()
.authenticationProvider(authProvider)
.buildClient();
这工作正常......但在某些情况下,从运行代码的地方,有一个代理,所以我需要设置代理来连接到互联网。我需要设置代理并将其传递给 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();
但问题是“ProxyOptions”不在 Maven 中,我不确定它属于哪个库。
任何人都可以提出一个想法吗?
最佳答案
正在更新答案...
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();
关于java - 使用 Java 将代理设置添加到 Microsoft Graph 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67738863/