Speech Service Authentication Issue on Bot Framework V4

81 Views Asked by At

Getting the following error while trying to get a token from Azure Speech Service. 'https://brazilsouth.api.cognitive.microsoft.com/sts/v1.0/issuetoken 401 (Access Denied)'.
Here is the way I'm requesting the token via JavaScript:
const res = await fetch('https://brazilsouth.api.cognitive.microsoft.com/sts/v1.0/issuetoken', { method: 'POST', headers: { Authorization: 'Bearer ' + 'MY_SPEECH_SERVICES_SUBSCRIPTION_KEY'}});
const { authorizationToken } = await res.json();
webSpeechPonyfillFactory = await window.WebChat.createCognitiveServicesSpeechServicesPonyfillFactory({ authorizationToken, region });

My bot works fine if I get a token manually via Windows PowerShell though.
What could be possibly wrong? Thx in advance

1

There are 1 best solutions below

0
On

Sharing a way to get the token via javascript. The 'data' variable will be storing the token. Thanks all for your support!

`<!DOCTYPE html>
<html>
<head>
    <title>JSSample</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>

<script type="text/javascript">
    $(function() {
        var params = {
            // Request parameters
        };

        $.ajax({
            url: "https://brazilsouth.api.cognitive.microsoft.com/sts/v1.0/issuetoken" + $.param(params),
            beforeSend: function(xhrObj){
                // Request headers
                xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","MY_SPEECH_SERVICES_SUBSCRIPTION_KEY");
            },
            type: "POST",
            // Request body
            data: "{body}",
        })
        .done(function(data) {
            alert(data);
        })
        .fail(function() {
            alert("error");
        });
    });
</script>
</body>
</html>`