C#IBM语音到文本使用APIKey获取令牌
基于上的示例, 我正在尝试使用WebSocket来使用IBM语音到文本API。 但是我在身份验证部分遇到了问题。 看起来现在IBM不再提供用户名/密码了。 只有一个api密钥 因此,我无法找到一种方法来添加该示例,即使用api获取令牌 知道如何使用WebSocket和IBM apikey进行身份验证吗? IBM文档似乎也不是最新的,因为他们的示例使用带有用户名和密码的CURL 我甚至在某个地方看到,我可以用“api”替换用户名,用我的api密钥替换密码。 但这不起作用,因为我从服务器收到未经授权的错误 也许我读错了,我应该传递一个令牌而不是密码。 但是我如何从我的带有WebSocket的APIkey中获得令牌呢 我可以使用HttpClient毫无问题地获取令牌。C#IBM语音到文本使用APIKey获取令牌,c#,authentication,websocket,ibm-watson,speech-to-text,C#,Authentication,Websocket,Ibm Watson,Speech To Text,基于上的示例, 我正在尝试使用WebSocket来使用IBM语音到文本API。 但是我在身份验证部分遇到了问题。 看起来现在IBM不再提供用户名/密码了。 只有一个api密钥 因此,我无法找到一种方法来添加该示例,即使用api获取令牌 知道如何使用WebSocket和IBM apikey进行身份验证吗? IBM文档似乎也不是最新的,因为他们的示例使用带有用户名和密码的CURL 我甚至在某个地方看到,我可以用“api”替换用户名,用我的api密钥替换密码。 但这不起作用,因为我从服务器收到未经授权
但是看起来我不能在Websocket中使用该令牌,之后,只有进一步的HttpClient调用。在一些帮助下,我终于找到了如何使用apiKey处理Websocket 我把代码贴在这里,以防别人需要
IamTokenData GetIAMToken(string apikey)
{
var wr = (HttpWebRequest)WebRequest.Create("https://iam.bluemix.net/identity/token");
wr.Proxy = null;
wr.Method = "POST";
wr.Accept = "application/json";
wr.ContentType = "application/x-www-form-urlencoded";
using (TextWriter tw = new StreamWriter(wr.GetRequestStream()))
{
tw.Write($"grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey={apikey}");
}
var resp = wr.GetResponse();
using (TextReader tr = new StreamReader(resp.GetResponseStream()))
{
var s = tr.ReadToEnd();
return JsonConvert.DeserializeObject<IamTokenData>(s);
}
}
IamTokenData tokenData = GetIAMToken([Your IamApiKey]);
CancellationTokenSource cts = new CancellationTokenSource();
ClientWebSocket clientWebSocket = new ClientWebSocket();
clientWebSocket.Options.Proxy = null;
clientWebSocket.Options.SetRequestHeader("Authorization", $"Bearer {token.AccessToken}");
// Make the sure the following URL is that one IBM pointed you to
Uri connection = new Uri($"wss://gateway-wdc.watsonplatform.net/speech-to-text/api/v1/recognize");
try
{
//await clientWebSocket.ConnectAsync(connection, cts.Token);
clientWebSocket.ConnectAsync(connection, cts.Token).GetAwaiter().GetResult();
Console.WriteLine("Connected!");
}
catch (Exception e)
{
Console.WriteLine("Failed to connect: " + e.ToString());
return null;
}
// ... Do what you need with the websocket after that ...
iamtoken数据获取iamtoken(字符串apikey)
{
var wr=(HttpWebRequest)WebRequest.Create(“https://iam.bluemix.net/identity/token");
wr.Proxy=null;
wr.Method=“POST”;
wr.Accept=“应用程序/json”;
wr.ContentType=“应用程序/x-www-form-urlencoded”;
使用(TextWriter tw=new StreamWriter(wr.GetRequestStream()))
{
Write($“grant_type=urn:ibm:params:oauth:grant type:apikey&apikey={apikey}”);
}
var resp=wr.GetResponse();
使用(TextReader tr=new StreamReader(resp.GetResponseStream())
{
var s=tr.ReadToEnd();
返回JsonConvert.DeserializeObject;
}
}
iamtokedata-tokenData=GetIAMToken([您的IamApiKey]);
CancellationTokenSource cts=新的CancellationTokenSource();
ClientWebSocket ClientWebSocket=新ClientWebSocket();
clientWebSocket.Options.Proxy=null;
SetRequestHeader(“授权”、$“承载{token.AccessToken}”);
//确保以下URL是IBM指向您的URL
Uri连接=新Uri($”wss://gateway-wdc.watsonplatform.net/speech-to-text/api/v1/recognize");
尝试
{
//wait clientWebSocket.ConnectAsync(连接,cts.Token);
ConnectAsync(连接,cts.Token).GetAwaiter().GetResult();
控制台。WriteLine(“已连接!”);
}
捕获(例外e)
{
Console.WriteLine(“连接失败:+e.ToString());
返回null;
}
// ... 在那之后用websocket做你需要的。。。