前提・実現したいこと
[IBM Watson Assistantで会話のやりとり]
WatsonのAssistantにWeb APIを利用して会話データ(messageなど)を取得したいです。
呼び出す処理はC#でかいています。
ネットでみつけたサンプルソースコードですがうまくいきませんでした。
ワークスペースはできています。
発生している問題・エラーメッセージ
"code" : 401 , "error" : "Not Authorized" , "description"
C# public class ConversationHelper { private readonly string _Server; private readonly NetworkCredential _NetCredential; public ConversationHelper(string workSpaceId, string userId, string password) { _Server = string.Format("https://gateway.watsonplatform.net/assistant/api/v1/workspaces/{0}/message?version={1}", workSpaceId, DateTime.Today.ToString("yyyy-MM-dd")); _NetCredential = new NetworkCredential(userId, password); } public async Task < string > GetResponse(string input, string context = null) { string req = null; if (string.IsNullOrEmpty(context)) req = "{\"input\": {\"text\": \"" + input + "\"}, \"alternate_intents\": true}"; else req = "{\"input\": {\"text\": \"" + input + "\"}, \"alternate_intents\": true}, \"context\": \"" + context + "\""; using(var handler = new HttpClientHandler { Credentials = _NetCredential }) using(var client = new HttpClient(handler)) { var cont = new HttpRequestMessage(); cont.Content = new StringContent(req.ToString(), Encoding.UTF8, "application/json"); var result = await client.PostAsync(_Server, cont.Content); return await result.Content.ReadAsStringAsync(); } } } class Program { static void Main(string[] args) { ConversationHelper helper = new ConversationHelper("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx", "xxxxxxxxxxxx"); var res = helper.GetResponse("hello").GetAwaiter().GetResult(); } }
試したこと
Watson Assistant API Explorerでは問題なく動作しています
あなたの回答
tips
プレビュー