Search code examples
httpsspeech-to-textbingbing-apiazure-cognitive-services

CppRestSDK https request not working


This is returning error code 401 or 500. Can somebody help me where I am going wrong?

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

Solution

  • Thanks all. I changed the code to following and i got the token!

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