在 Java 中向 Microsoft Graph 客户端添加代理设置
Adding a proxy settings to Microsoft Graph Client in Java
我正在尝试实现 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();
但问题是 Maven 中没有“ProxyOptions”,我不确定它属于哪个库。
任何人都可以提出一个想法。
正在更新答案...
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();
如果可以使用系统属性,则尝试通过
进行配置
https.proxyHost 和 https.proxyPort
我正在尝试实现 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();
但问题是 Maven 中没有“ProxyOptions”,我不确定它属于哪个库。
任何人都可以提出一个想法。
正在更新答案...
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();
如果可以使用系统属性,则尝试通过
进行配置https.proxyHost 和 https.proxyPort