我试图通过Java应用程序访问Watson Conversation Service。因此我在Bluemix上创建了一个写了一个小应用程序的服务。
package de.kkh.comp.WatsonDemo;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import com.ibm.watson.developer_cloud.conversation.v1.ConversationService;
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest;
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse;
public class App {
private static final String USERNAME = "{USERNAME}";
private static final String PASSWORD = "{PASSWORD}";
private static final String WORKSPACE_ID = "{WORKSPACE_ID}";
public static void main(String[] args) {
ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2017_02_03);
service.setUsernameAndPassword(USERNAME, PASSWORD);
MessageRequest newMessage = new MessageRequest.Builder().inputText("Hallo").context(new HashMap<String,Object>()).build();
MessageResponse response = service.message(WORKSPACE_ID, newMessage).execute();
System.out.println(response);
}
}
我希望我能得到一个简单的Watson服务答案。
如果我运行应用程序,我得到了一个Not Authorized Exception,尽管我使用的是Bluemix提供的凭据。
Aug 02, 2017 7:56:19 PM okhttp3.internal.platform.Platform log
INFORMATION: --> POST https://gateway.watsonplatform.net/conversation/api/v1/workspaces/{WORKSPACE_ID}/message?version=2017-02-03 http/1.1 (39-byte body)
Aug 02, 2017 7:56:20 PM okhttp3.internal.platform.Platform log
INFORMATION: <-- 401 Not Authorized https://gateway.watsonplatform.net/conversation/api/v1/workspaces/{WORKSPACE_ID}/message?version=2017-02-03 (214ms, unknown-length body)
Aug 02, 2017 7:56:20 PM com.ibm.watson.developer_cloud.service.WatsonService processServiceCall
SCHWERWIEGEND: POST https://gateway.watsonplatform.net/conversation/api/v1/workspaces/{WORKSPACE_ID}/message?version=2017-02-03, status: 401, error: Not Authorized
Exception in thread "main" com.ibm.watson.developer_cloud.service.exception.UnauthorizedException: Unauthorized: Access is denied due to invalid credentials
at com.ibm.watson.developer_cloud.service.WatsonService.processServiceCall(WatsonService.java:492)
at com.ibm.watson.developer_cloud.service.WatsonService$2.execute(WatsonService.java:254)
at de.kkh.comp.WatsonDemo.App.main(App.java:26)
我没有任何线索为什么我得到这个例外。任何想法?
答案 0 :(得分:5)
问题解决了。
我使用了德国的位置。因此,API-Endpoint需要为https://gateway-fra.watsonplatform.net/conversation/api,而不是默认URL。
ConversationService的构造函数使用默认URL,即https://gateway.watsonplatform.net/conversation/api。要更改端点,必须致电:
service.setEndPoint("https://gateway-fra.watsonplatform.net/conversation/api")
在我这样做之后,一切正常。
答案 1 :(得分:2)
我遇到了同样的问题 - 我的问题是,我打电话给的api网址是错误的位置。例如,如果您使用德国,则正确的API端点为:https://gateway-fra.watsonplatform.net/conversation/api