2016-12-01 1 views
3

오류 코드 401 또는 500이 반환됩니다. 누군가 내가 잘못 가고있는 곳에서 나를 도울 수 있습니까?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 임상에서 작동하지만 이유를 얻지 못했습니다! – ryadav

답변

0

는 여기에 내가 시도 마지막 시간을 근무 요청 (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" 
    } 
}, 
+0

Fiddler를 사용하여 요청 및 응답을 관찰하십시오. – BSalita

+0

감사! 지금부터는 액세스 토큰을 얻을 수 있습니다. 나는이 json에게 시험해 볼 것이다. – ryadav

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

전체 응답 기관은 토큰이 포함 된 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; 
}); 

}); 
}; 
관련 문제