CppRestSDK https请求无效

时间:2016-12-01 17:57:45

标签: https speech-to-text bing bing-api microsoft-cognitive

这将返回错误代码401500。在我出错的地方,有人可以帮助我吗?

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);

2 个答案:

答案 0 :(得分:1)

谢谢大家。我把代码更改为以下,我得到了令牌!

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;
});

});
};

答案 1 :(得分:0)

这是我上次尝试的请求的通用JSON表示(2016年9月)。你的要求看起来很不一样。摘自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"
  }
},