我正在尝试让我的php脚本向语音转文本API发送curl请求。
我在Watson控制台中配置了API,并挂载了发送音频和apikey的脚本。
$url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize';
$file = '@files/WhatsApp Audio 2019-01-25 at 12.17.28 (1).flac';
$fields = array(
'file' => $file,
'continuous' => true
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "apikey:KEY");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: audio/flac'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Transfer-Encoding: chunked'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_MAX_SEND_SPEED_LARGE, 40000);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
$executed = curl_exec($ch);
var_dump($executed);我总是得到这样的结果:
string(37) "{"code":401,"error":“未授权”}“
发布于 2019-01-28 09:30:12
在您的脚本中,您拥有apikey:KEY作为凭证。我知道这听起来很明显,但最好仔细检查一下:您是否已将其替换为来自IBM控制台的实际api密钥?如果你使用KEY作为你的密钥,那么,你得到401未经授权也就不足为奇了:)
还要注意,您的api密钥与一个特定的网关URL相关联,您还可以在IBM Cloud控制台中检索密钥的相同位置找到它(请参见屏幕截图)。这就是您的curl命令需要使用的URL。例如,在我的示例中,由于我创建了位置为London的应用程序,因此url是gateway-lon.watsonplatform.net。

https://stackoverflow.com/questions/54394137
复制相似问题