2017-08-02 44 views
0

我試圖通過Java應用程序訪問Watson對話服務。因此我在Bluemix上創建了一個服務,寫了一個小應用程序。Watson對話服務 - 未經授權:由於憑據無效導致訪問被拒絕

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

我希望我能得到沃森服務的簡單答案。

如果我運行該應用程序,我得到了一個未授權的例外,儘管我使用由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

您是否檢查拼寫錯誤?資本?你有沒有使用正確的課程,方法或格式?添加更多詳細信息,並嘗試一兩個谷歌搜索。 – Rigidity

+0

我已經複製粘貼了證書,並且類,方法......如服務API中所述。我用Google搜索了一下,但找不到任何有用的東西。這就是我問的原因。你想知道什麼細節? –

+0

它可能是API已更改?如果我們看[當前版本](https://github.com/watson-developer-cloud/java-sdk/blob/develop/conversation/src/main/java/com/ibm/watson/developer_cloud/conversation/) v1/ConversationService.java#L95),日期字符串爲+3個月,然後是您運行的庫版本。 –

回答

1

我有同樣的問題 - 我的問題是,我打電話的API網址是錯誤的位置。例如,如果您使用德國,正確的API端點爲:https://gateway-fra.watsonplatform.net/conversation/api

+0

聽起來合法。你是對的。我使用德國的位置。 我嘗試與另一個端點。謝謝。 –

+0

嘿。正如你所說,我已經改變了端點。這很好。感謝您的幫助。 :) –

+0

很高興我能幫忙! :) –

相關問題