CppRestSDK https 请求不工作
CppRestSDK https request not working
这将返回错误代码 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);
这是我上次尝试(2016 年 9 月)时有效的请求的通用 JSON 表示。你的要求看起来很不一样。摘自Woundify settings file
{
"name": "BingSpeechToTextService",
"classInterface": "BingServices.ISpeechToTextService",
"request": {
"method": "post", // { "get" | "post" | <custom> }
"preferChunkedEncodedRequests": false,
"uri": {
"scheme": "https",
"host": "speech.platform.bing.com",
"path": "recognize",
"query": "scenarios=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}"
},
"headers": [
{
"Name": "Accept",
"Accept": "application/json"
},
{
"Name": "BearerAuthentication",
"BearerAuthentication": {
"type": "bearer", // { basic | bearer | <custom> }
"clientID": "",
"clientSecret": "",
"scope": "https://speech.platform.bing.com",
"uri": "https://oxford-speech.cloudapp.net/token/issueToken",
"grant": "grant_type=client_credentials&client_id={clientID}&client_secret={clientSecret}&scope={scope}"
}
},
{
"Name": "Content-Type",
"ContentType": "audio/wav; codec=\"audio/pcm\"; samplerate={sampleRate}"
}
],
"data": {
"type": "binary" // { ascii | base64 | binary | json | raw | string | urlencode }
}
},
"response": {
"missingResponse": "whatever",
"jsonPath": "results[0].name"
}
},
请注意,现在有一个更简单的令牌发行 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);
});
}
整个响应主体都是令牌,这与旧方案不同,旧方案的 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;
});
});
};
这将返回错误代码 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);
这是我上次尝试(2016 年 9 月)时有效的请求的通用 JSON 表示。你的要求看起来很不一样。摘自Woundify settings file
{
"name": "BingSpeechToTextService",
"classInterface": "BingServices.ISpeechToTextService",
"request": {
"method": "post", // { "get" | "post" | <custom> }
"preferChunkedEncodedRequests": false,
"uri": {
"scheme": "https",
"host": "speech.platform.bing.com",
"path": "recognize",
"query": "scenarios=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}"
},
"headers": [
{
"Name": "Accept",
"Accept": "application/json"
},
{
"Name": "BearerAuthentication",
"BearerAuthentication": {
"type": "bearer", // { basic | bearer | <custom> }
"clientID": "",
"clientSecret": "",
"scope": "https://speech.platform.bing.com",
"uri": "https://oxford-speech.cloudapp.net/token/issueToken",
"grant": "grant_type=client_credentials&client_id={clientID}&client_secret={clientSecret}&scope={scope}"
}
},
{
"Name": "Content-Type",
"ContentType": "audio/wav; codec=\"audio/pcm\"; samplerate={sampleRate}"
}
],
"data": {
"type": "binary" // { ascii | base64 | binary | json | raw | string | urlencode }
}
},
"response": {
"missingResponse": "whatever",
"jsonPath": "results[0].name"
}
},
请注意,现在有一个更简单的令牌发行 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);
});
}
整个响应主体都是令牌,这与旧方案不同,旧方案的 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;
});
});
};