CppRestSDK https请求不起作用

问题描述:

这是返回的错误代码401500。有人能帮助我,我哪里出错了吗?CppRestSDK https请求不起作用

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

我删除了您的客户端密钥,您可能需要重新生成它。 –

+0

谢谢。没有得到理由,虽然相同的查询适用于其他JS clinet! – ryadav

下面是我上次尝试使用的请求的一般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" 
    } 
}, 
+0

使用提琴手来观察您的请求和回应。 – BSalita

+0

谢谢!因为现在我能够获得访问令牌。我会试试这个json。 – ryadav

请注意,现在有一个更简单的令牌发布网址。你的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响应令牌。

+0

谢谢!我从 - https://www.microsoft.com/cognitive-services/en-us/speech-api/documentation/api-reference-rest/bingvoicerecognition获得了相同的结果。有用 :) – ryadav

谢谢大家。我将代码更改为以下代码,并获得了令牌!

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

}); 
};