且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

语音识别服务器REST API返回500(内部服务器错误)

更新时间:2022-10-20 16:40:57

  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