语音识别服务器REST API返回500(内部服务器错误) [英] Speech Recognition Server REST API returning 500 (internal server error)
问题描述
我无法搞清楚api文档。 我已经进行了综合api调用,所以我知道我得到了一个很好的访问令牌。 现在无论我尝试什么,我都会从识别服务器获得500回。
我正在使用标准的"whatstheweatherlike.wav"。测试文件以消除这个问题。
这是使用Microsoft的C ++卡萨布兰卡的代码:
#include< cpprest / http_client.h>
#include< cpprest / filestream.h>
$
使用命名空间实用程序; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //字符串转换等常用实用程序使用命名空间Web进行
; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // URI等常用功能。
using namespace web :: http; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //通用HTTP功能
使用命名空间web :: http :: client; &NBSP; &NBSP; &NBSP; &NBSP; // HTTP客户端功能使用namespace concurrency :: streams;
; &NBSP; &NBSP; &NBSP; //异步流
使用命名空间实用程序::转换;
#include< fstream>
# include< string>
命名空间{
std :: vector< unsigned char> readAllBytes(std :: string filename)
{
std :: ifstream ifs(filename.c_str(),std :: ios :: binary | std :: ios :: ate);
std :: ifstream :: pos_type pos = ifs.tellg();
std :: vector< unsigned char>结果(pos);
$
ifs.seekg(0,std :: ios :: beg ); $
ifs.read((char *)& result [0],pos);
返回结果;
}
}
int main(int argc,char ** argv)
{
auto clientId = U(""); &NBSP; &NBSP; //删除到论坛发帖
auto clientSecret = U(""); //删除到论坛发帖
auto speechHost = U(" https://speech.platform.bing.com" ); $
uri_builder ub;
ub.append_query(U("grant_type")),U("client_credentials"));
ub.append_query(U(" client_id"),clientId);
ub.append_query(U ("client_secret"),clientSecret);
$
ub.append_query(U(" scope"),speechHost);
auto accessTokenPostData = to_utf8string(ub.query());
http_client tokenClient(U(" https://oxford-speech.cloudapp.net"));
http_request tokenRequest(methods :: POST);
tokenRequest.set_request_uri(uri(U(&/ token / issueToken")));
$
tokenRequest.set_body(accessTokenPostData);
$
http_headers tokenHeaders;
tokenHeaders [U("Content-type")] = U("application / x-www" -form-urlencoded");
tokenRequest.headers()= tokenHeaders;
tokenClient.request(tokenRequest)
.then([](http_response response) - > pplx :: task< http_response>
{
if(response.status_code()== status_codes :: OK)
{
auto accessTokenJSON = response.extract_json()。get();
auto accessTokenName = U(" access_token");
auto accessTokenExpiresInName = U(" expires_in" ); $
if(accessTokenJSON.has_field(accessTokenName))
{
auto accessToken = accessTokenJSON [accessTokenName] .as_string();
http_client recognitionClient(U("https://speech.platform.bing.com"));
http_request reco gnitionRequest(methods :: POST);
$
auto appGeneratedRequestId = U(" 1d4b6030-9099 -11e0-91e4-0800200c9a22");
auto deviceId = U(" 1d4b6030-9099-11e0-91e4-0800200c9a00" ); $
uri_builder builder;
builder.set_host(U(" https://speech.platform.bing.com"));
builder.set_path(U(" / recognition"));;
builder.append_query( U("版本"),U("3.0"));
builder.append_query(U(") requestid"),appGeneratedRequestId);
$
builder.append_query(U("appid"),U(" D4D52672-91D7) -4C74-8AD8-42B1D98141A5"));
builde r.append_query(U("格式"),U("json"));
builder.append_query(U ("locale"),U("en-US"));
builder.append_query(U(") device.os"),U(" Windows OS"));
$
builder.append_query(U" scenario" ),U("ulm"));
builder.append_query(U("instanceid"),deviceId);
recognitionRequest.set_request_uri(builder.to_uri());
recognitionRequest.set_body(readAllBytes(" whatstheweatherlike.wav)));
http_headers标头;
标头[U("Content-type")] = U("audio / wav;编解码器="音频/ WAV"; samplerate = 16000");
标题[U("授权")] = U("承载")+ accessToken;
$
recognitionRequest.headers()= headers;
返回recognitionClient.request(recognitionRequest);
}
}
返回pplx :: task_from_result(http_response());
})
.then([](http_response response)
{
if(response.status_code()== status_codes :: OK)
{
auto body = response.extract_json()。get() ;
$
}
})。wait();
#include< windows.h>
$
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,INT)
{
返回main(__ argc,__ argv);
}
Hi RatherSleepy,
我不确定这是否正确,你能帮忙更新以下一行吗?
headers [U("Content-type")] = U(" audio / wav; codec ="audio / wav"; samplerate = 16000");
试试这个
headers [U("Content-type")] = U(" audio / wav; samplerate = 8000 ");
你确定你有500个内部错误吗?非常感谢。
Jia
跨度>
I'm having trouble figuring out the api documentation. I've got the synthesis api call working so I know that I'm getting a good access token. Right now I get a 500 back from the recognition server no matter what I try.
I'm using the standard "whatstheweatherlike.wav" test file to eliminate that as a problem.
Here is my code using Microsoft's C++ Casablanca:
#include <cpprest/http_client.h>#include <cpprest/filestream.h>
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
using namespace utility::conversions;
#include <fstream>
#include <string>
namespace {
std::vector<unsigned char> readAllBytes(std::string filename)
{
std::ifstream ifs(filename.c_str(), std::ios::binary | std::ios::ate);
std::ifstream::pos_type pos = ifs.tellg();
std::vector<unsigned char> result(pos);
ifs.seekg(0, std::ios::beg);
ifs.read((char *)&result[0], pos);
return result;
}
}
int main(int argc, char ** argv)
{
auto clientId = U(""); // removed to post on forum
auto clientSecret = U(""); // removed to post on forum
auto speechHost = U("https://speech.platform.bing.com");
uri_builder ub;
ub.append_query(U("grant_type"), U("client_credentials"));
ub.append_query(U("client_id"), clientId);
ub.append_query(U("client_secret"), clientSecret);
ub.append_query(U("scope"), speechHost);
auto accessTokenPostData = to_utf8string(ub.query());
http_client tokenClient(U("https://oxford-speech.cloudapp.net"));
http_request tokenRequest(methods::POST);
tokenRequest.set_request_uri(uri(U("/token/issueToken")));
tokenRequest.set_body(accessTokenPostData);
http_headers tokenHeaders;
tokenHeaders[U("Content-type")] = U("application/x-www-form-urlencoded");
tokenRequest.headers() = tokenHeaders;
tokenClient.request(tokenRequest)
.then([](http_response response)-> pplx::task<http_response>
{
if (response.status_code() == status_codes::OK)
{
auto accessTokenJSON = response.extract_json().get();
auto accessTokenName = U("access_token");
auto accessTokenExpiresInName = U("expires_in");
if (accessTokenJSON.has_field(accessTokenName))
{
auto accessToken = accessTokenJSON[accessTokenName].as_string();
http_client recognitionClient(U("https://speech.platform.bing.com"));
http_request recognitionRequest(methods::POST);
auto appGeneratedRequestId = U("1d4b6030-9099-11e0-91e4-0800200c9a22");
auto deviceId = U("1d4b6030-9099-11e0-91e4-0800200c9a00");
uri_builder builder;
builder.set_host(U("https://speech.platform.bing.com"));
builder.set_path(U("/recognize"));
builder.append_query(U("version"), U("3.0"));
builder.append_query(U("requestid"), appGeneratedRequestId);
builder.append_query(U("appid"), U("D4D52672-91D7-4C74-8AD8-42B1D98141A5"));
builder.append_query(U("format"), U("json"));
builder.append_query(U("locale"), U("en-US"));
builder.append_query(U("device.os"), U("Windows OS"));
builder.append_query(U("scenarios"), U("ulm"));
builder.append_query(U("instanceid"), deviceId);
recognitionRequest.set_request_uri(builder.to_uri());
recognitionRequest.set_body(readAllBytes("whatstheweatherlike.wav"));
http_headers headers;
headers[U("Content-type")] = U("audio/wav; codec="audio/wav"; samplerate=16000");
headers[U("Authorization")] = U("Bearer ") + accessToken;
recognitionRequest.headers() = headers;
return recognitionClient.request(recognitionRequest);
}
}
return pplx::task_from_result(http_response());
})
.then([](http_response response)
{
if (response.status_code() == status_codes::OK)
{
auto body = response.extract_json().get();
}
}).wait();
#include <windows.h>
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
return main(__argc, __argv);
}
Hi RatherSleepy,
I am not so sure if this is right, could you help to update the following line?
headers[U("Content-type")] = U("audio/wav; codec="audio/wav"; samplerate=16000");
Try this
headers[U("Content-type")] = U("audio/wav; samplerate=8000");
Are you sure you got 500 internal error? Thanks a lot.
Jia
这篇关于语音识别服务器REST API返回500(内部服务器错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!