IBM Watson Assistant - HTTPS 代理不工作?

IBM Watson Assistant - HTTPS proxy not working?

我目前正在需要 HTTPS 代理的公司网络中试验一种奇怪的行为。 我正在使用以下版本的 IBM Watson Assistant API:

<dependency>
  <groupId>com.ibm.watson</groupId>
  <artifactId>ibm-watson</artifactId>
  <version>8.6.0</version>
</dependency>

(也用 v9.0.2 测试过)

按照文档,下面的代码似乎没有正确考虑代理,因为我最终遇到错误,只是说:“从令牌服务获取访问令牌时出错:"

import java.net.InetSocketAddress;
import java.net.Proxy;
import com.ibm.cloud.sdk.core.http.HttpConfigOptions;
import com.ibm.cloud.sdk.core.http.ServiceCall;
import com.ibm.cloud.sdk.core.security.IamAuthenticator;
import com.ibm.watson.assistant.v2.Assistant;
import com.ibm.watson.assistant.v2.model.CreateSessionOptions;
import com.ibm.watson.assistant.v2.model.SessionResponse;

[...]

IamAuthenticator authenticator = new IamAuthenticator(apiKey);
Assistant assistant = new Assistant(version, authenticator);
assistant.setServiceUrl(url);

HttpConfigOptions httpOptions = new HttpConfigOptions.Builder()
        .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, (Integer.parseInt(proxyPort)))))
        .loggingLevel(HttpConfigOptions.LoggingLevel.BASIC)
        .build();
assistant.configureClient(httpOptions);

CreateSessionOptions options = new CreateSessionOptions.Builder(assistantId).build();
ServiceCall<SessionResponse> serviceCall = assistant.createSession(options);

但是,如果我删除 httpOptions 并改为设置系统属性,它会起作用并创建我的会话:

System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", proxyPort);

那么,您知道初始代码可能有什么问题吗?我还发现了一些关于 HTTPS 代理的主题,底层库 okHttp 不支持,但它看起来不太有说服力。

此外,请注意,过去我使用的是不同的版本,没有这样的问题。我切换到 API.

的 v1 到 v2
<dependency>
    <groupId>com.ibm.watson.developer_cloud</groupId>
    <artifactId>java-sdk</artifactId>
    <version>6.13.0</version>
</dependency>

编辑

我回滚到 java-sdk(最新版本),而不是 ibm-watson:

<dependency>
    <groupId>com.ibm.watson.developer_cloud</groupId>
    <artifactId>java-sdk</artifactId>
    <version>6.14.2</version>
</dependency>

代码很相似...

import java.net.InetSocketAddress;
import java.net.Proxy;
import com.ibm.watson.developer_cloud.assistant.v2.Assistant;
import com.ibm.watson.developer_cloud.assistant.v2.model.CreateSessionOptions;
import com.ibm.watson.developer_cloud.assistant.v2.model.SessionResponse;
import com.ibm.watson.developer_cloud.http.HttpConfigOptions;
import com.ibm.watson.developer_cloud.http.ServiceCall;
import com.ibm.watson.developer_cloud.service.security.IamOptions;

[...]

IamOptions iamOptions = new IamOptions.Builder().apiKey(apiKey).build();
Assistant assistant = new Assistant(version, iamOptions);
assistant.setEndPoint(url);

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, (Integer.parseInt(proxyPort))));
HttpConfigOptions httpOptions = new HttpConfigOptions.Builder().proxy(proxy).build();
assistant.configureClient(httpOptions);

CreateSessionOptions options = new CreateSessionOptions.Builder(assistantId).build();
ServiceCall<SessionResponse> serviceCall = assistant.createSession(options);

并且通过 HTTPS 代理与 API 的连接再次正常工作。参数相同。所以这绝对不是 okHttp 的问题,而是 ibm-watson lib 的问题,对吧?

除非我遗漏了一些明显的东西,否则我会将其报告为一个问题。

谢谢你的时间。

干杯

问题可能如下: 您必须在身份验证器和服务 authenticator.setProxy(proxy) 中使用相同的代理; 这是示例代码:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("104.xx.xx.xx", Integer.parseInt("443")));
IamAuthenticator authenticator = new IamAuthenticator.Builder()
      .apikey("XXXXXXXXXXXXXXXXXXXXXXXXXXX")
      .url("https://iam.cloud.ibm.com/identity/token")
      .disableSSLVerification(true)
      .build();
authenticator.setProxy(proxy);com.ibm.watson.assistant.v2.Assistant anAssistant = new com.ibm.watson.assistant.v2.Assistant("2020-09-24", authenticator);anAssistant.setServiceUrl("https://api.eu-de.assistant.watson.cloud.ibm.com/instances/437b4786-68e2-404d-850b-748b663a5314");Map<String, String> headers = new HashMap<>();
headers.put("X-Watson-Learning-Opt-Out", "true");
anAssistant.setDefaultHeaders(headers);HttpConfigOptions httpConfigOptions = new HttpConfigOptions.Builder().proxy(proxy)
      .loggingLevel(HttpConfigOptions.LoggingLevel.BODY)
      .disableSslVerification(true)
      .build();
anAssistant.configureClient(httpConfigOptions);CreateSessionOptions createSessionOptions = new CreateSessionOptions.Builder().assistantId("aa265a2f-e9ed-43c7-xxxx-xxxxxxxxxxxxx").build();
anAssistant.createSession(createSessionOptions).execute();
SessionResponse sessionResponse = anAssistant.createSession(createSessionOptions).execute().getResult();