2016-12-01 69 views
3

這是返回的錯誤代碼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

回答

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" 
    } 
}, 
+0

使用提琴手來觀察您的請求和迴應。 – BSalita

+0

謝謝!因爲現在我能夠獲得訪問令牌。我會試試這個json。 – ryadav

0

請注意,現在有一個更簡單的令牌發佈網址。你的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

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

}); 
};