【问题标题】:Watson Conversation Service - Unauthorized: Access is denied due to invalid credentialsWatson 对话服务 - 未经授权:由于凭据无效,访问被拒绝
【发布时间】:2017-08-02 18:21:05
【问题描述】:

我尝试通过 Java 应用程序访问 Watson 对话服务。因此,我在 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 服务的简单回答。

如果我运行应用程序,我得到一个未授权异常,尽管我使用的是 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)

我不知道为什么我会得到这个异常。有什么想法吗?

【问题讨论】:

  • 您检查拼写错误了吗?大写?您是否使用了正确的类、方法或格式?添加更多详细信息并尝试一两次谷歌搜索。
  • 我已经复制粘贴了凭据,并且类、方法……如服务 API 中所述。我用谷歌搜索,但找不到任何有用的东西。这就是我问的原因。你想知道什么细节?
  • 会不会是 API 变了?如果我们查看the current version,日期字符串比您正在运行的库版本晚 +3 个月。

标签: java ibm-cloud watson-conversation


【解决方案1】:

问题解决了。

我使用了德国位置。因此 API-Endpoint 需要是 https://gateway-fra.watsonplatform.net/conversation/api,而不是 Default-URL。

ConversationService 的构造函数使用默认 URL,即https://gateway.watsonplatform.net/conversation/api。要更改端点,有必要调用: service.setEndPoint("https://gateway-fra.watsonplatform.net/conversation/api")

在我这样做之后,一切正常。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题 - 我的问题是,我调用的 api URL 的位置错误。例如,如果您使用的是德国,则正确的 API 端点是:https://gateway-fra.watsonplatform.net/conversation/api

    【讨论】:

    • 听起来合法。你是对的。我使用德国的位置。我尝试使用另一个端点。谢谢。
    • 嘿。正如你所说,我已经改变了端点。效果很好。感谢您的帮助。 :)
    • 很高兴能帮上忙! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 2012-04-20
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多