CppRestSDK https请求不工作

CppRestSDK https请求不工作,https,speech-to-text,bing,bing-api,microsoft-cognitive,Https,Speech To Text,Bing,Bing Api,Microsoft Cognitive,这将返回错误代码401或500。有人能帮我找出哪里出了问题吗 http_client client(L"https://oxford-speech.cloudapp.net/token/issueToken/"); uri_builder query; query.append_query(L"grant_type", L"client_credentials"); query.append_query(L"client_id", L"test-app"); query.a

这将返回错误代码
401
500
。有人能帮我找出哪里出了问题吗

http_client client(L"https://oxford-speech.cloudapp.net/token/issueToken/");    
uri_builder query;
query.append_query(L"grant_type", L"client_credentials");       
query.append_query(L"client_id", L"test-app");
query.append_query(L"client_secret", L"<client secret goes here>");
query.append_query(L"scope", L"https://speech.platform.bing.com");
query.append_query(L"content_type", L"application/x-www-form-urlencoded");

http_request msg(methods::POST);
msg.headers().set_content_type(L"application/x-www-form-urlencoded");
msg.set_request_uri(query.to_string());

std::wstring str = msg.to_string();
return client.request(msg);
http_客户端(L)https://oxford-speech.cloudapp.net/token/issueToken/");    
uri_生成器查询;
追加查询(L“授权类型”,L“客户凭证”);
查询。追加查询(L“客户端id”,L“测试应用程序”);
追加查询(L“client\u secret”,L“”);
查询。追加查询(L“范围”,L”https://speech.platform.bing.com");
查询。追加查询(L“内容类型”,L“应用程序/x-www-form-urlencoded”);
http_请求消息(方法::POST);
msg.headers().set_content_type(L“application/x-www-form-urlencoded”);
msg.set_request_uri(query.to_string());
std::wstring str=msg.to_string();
返回client.request(msg);

这是我上次尝试(2016年9月)的一个请求的通用JSON表示。你的要求看起来很不一样。摘自

{
“名称”:“BingSpeechToTextService”,
“classInterface”:“BingServices.ISpeechToTextService”,
“请求”:{
“方法”:“post”、/{“get”|“post”|}
“首选代码请求”:false,
“uri”:{
“方案”:“https”,
“主持人”:“speech.platform.bing.com”,
“路径”:“识别”,
“查询”:“场景=smd&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5&locale={locale}&device.os=wp7&version=3.0&format=json&instanceid=565D69FF-E928-4B7E-87DA-9A750B96D9E3&requestid={guid}”
},
“标题”:[
{
“名称”:“接受”,
“接受”:“应用程序/json”
},
{
“名称”:“BeareAuthentication”,
“BeareAuthentication”:{
“类型”:“承载物”、/{basic |承载物|}
“clientID”:“,
“客户机密”:“,
“范围”:https://speech.platform.bing.com",
“uri”:https://oxford-speech.cloudapp.net/token/issueToken",
“grant”:“grant_type=client_凭据&client_id={clientID}&client_secret={clientSecret}&scope={scope}”
}
},
{
“名称”:“内容类型”,
“ContentType”:“音频/wav;编解码器=\“音频/pcm\”采样器={samplerate}”
}
],
“数据”:{
“类型”:“二进制”//{ascii | base64 |二进制| json |原始|字符串| urlencode}
}
},
“答复”:{
“missingResponse”:“随便什么”,
jsonPath:“结果[0]。名称”
}
},

请注意,现在有一个更简单的令牌发布URL。你的C++代码看起来会像这样:

pplx::task<string_t> getToken()
{
    http_client client(L"https://api.cognitive.microsoft.com/sts/v1.0/issueToken");
    http_request req(methods::POST);
    req.headers().add(L"Ocp-Apim-Subscription-Key", YOUR_API_KEY);
    return client.request(req).then([=](http_response response) -> pplx::task<string_t>
    {
        return response.extract_string(true);
    });
}
pplx::task getToken()
{
http_客户端(L)https://api.cognitive.microsoft.com/sts/v1.0/issueToken");
http_请求请求请求(方法::POST);
req.headers().add(L“Ocp Apim订阅密钥”,即您的API密钥);
返回client.request(req).then([=](http_响应)->pplx::task
{
返回响应。提取_字符串(true);
});
}

与旧方案不同,整个响应主体是令牌,旧方案有一个包含令牌的JSON响应。

谢谢大家。我把代码改成了following,我得到了令牌

pplx::task<void> getAccessToken()
{
istream bodyStream;
http_client client(L"https://api.cognitive.microsoft.com/sts/v1.0/issueToken");
http_request req(methods::POST);
req.headers().add(L"Ocp-Apim-Subscription-Key", L"YOUR_KEY");

return client.request(req)

.then([](http_response response)
{
    if (response.status_code() != status_codes::OK)
    {
        return pplx::task_from_result();
    }
    istream bodyStream = response.body();
    container_buffer<std::string> inStringBuffer;       
    return bodyStream.read_line(inStringBuffer)

.then([inStringBuffer](size_t bytesRead)
{
    const std::string &text = inStringBuffer.collection();
    std::cout << text;
});

});
};
pplx::任务getAccessToken()
{
峡流体流;
http_客户端(L)https://api.cognitive.microsoft.com/sts/v1.0/issueToken");
http_请求请求请求(方法::POST);
添加(L“Ocp Apim订阅密钥”,L“您的密钥”);
返回客户端请求(req)
。然后([](http_响应)
{
if(response.status\u code()!=status\u code::OK)
{
从_result()返回pplx::task_;
}
istream bodyStream=response.body();
集装箱缓冲装置仪表缓冲装置;
返回bodyStream.read_行(inStringBuffer)
.然后([inStringBuffer](大小字节读取)
{
const std::string&text=inStringBuffer.collection();

std::我是否删除了你的客户端密码,你可能想重新生成它。谢谢。虽然同一查询在其他JS clinet中有效,但没有得到原因!请使用Fiddler观察你的请求和响应。谢谢!从现在起,我可以获得访问令牌。我将尝试使用此json。谢谢!我从-,获得了相同的结果。它有效:)
pplx::task<void> getAccessToken()
{
istream bodyStream;
http_client client(L"https://api.cognitive.microsoft.com/sts/v1.0/issueToken");
http_request req(methods::POST);
req.headers().add(L"Ocp-Apim-Subscription-Key", L"YOUR_KEY");

return client.request(req)

.then([](http_response response)
{
    if (response.status_code() != status_codes::OK)
    {
        return pplx::task_from_result();
    }
    istream bodyStream = response.body();
    container_buffer<std::string> inStringBuffer;       
    return bodyStream.read_line(inStringBuffer)

.then([inStringBuffer](size_t bytesRead)
{
    const std::string &text = inStringBuffer.collection();
    std::cout << text;
});

});
};