简体   繁体   中英

IBM Watson Assistant - HTTPS proxy not working?

I am currently experimenting a strange behavior, within a corporate network that requires HTTPS proxy. I am using the following version of IBM Watson Assistant API:

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

(also tested with v9.0.2)

Following the documentation, the code bellow does not seem to take properly the proxy into account as I end up with an error, simply saying: " Error while fetching access token from token service: "

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);

However, if I remove the httpOptions and set the system properties instead, it works and my session is created:

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

So, do you have any idea about what could be wrong with the initial code? I have also found some topics about HTTPS proxy not beeing supported by the underlying library okHttp, but it did not look very convincing.

Also, please note that in the past I was using instead a different version, with no such issue. I switched to move from v1 to v2 of the API.

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

EDIT

I rolled back to java-sdk (latest version), instead of ibm-watson:

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

The code is very similar...

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);

And the connection to the API through HTTPS proxy is working fine again. Parameters are the same. So it is definitely not an issue with okHttp but more with the ibm-watson lib, right?

Unless I am missing something obvious, I'll report this as an issue.

Thanks for you time.

Cheers

the issue is probably the following: you have to use the same proxy in the authenticator and in the service authenticator.setProxy(proxy); Here is sample code:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM