Text to speech API Issue

Bhagwan Rajput 0 Reputation points
2024-08-22T12:16:22.0966667+00:00

Hi Team, We are using Microsoft azure api text to speech for (Android & IOS) but it is not working we are getting error code 403 in response. Could you please check and let us know what is the issue . Here is the api integration code could you please check it once : -

    func synthesize(text: String,

                    outputFormat: TTSAudioOutputFormat = .riff16Khz16BitMonoPcm,

                    lang: String = "eastus",

                    gender: TTSGender = .female,

                    callback: @escaping (Data) -> ()) {

        self.authentication.getAccessToken { (accessToken: String) in

            let message = ""

            let encoding = String.Encoding.utf8

            TTSHttpRequest.submit(withUrl: TTSSynthesizer.ttsServiceUri,

                                  andHeaders: [

                                    "Content-Type": "application/ssml+xml",

                                    "X-Microsoft-OutputFormat": outputFormat.rawValue,

                                    "Authorization": "Bearer " + accessToken,

                                    "User-Agent": "TTSiOS",

                                    "content-length": "(message.lengthOfBytes(using: encoding))"

                ],

                                  andBody: message.data(using: encoding)) { (c: TTSHttpRequest.Callback) in

                                    guard let data = c.data else { return }

                                    callback(data)

            }

        

        }

    }

Thanks in advance.

Getting below response from API
Printing description of httpResponse:

<NSHTTPURLResponse: 0x600000b91d60> { URL: https://eastus.tts.speech.microsoft.com/cognitiveservices/v1 } { Status Code: 403, Headers {

    Date =     (

        "Thu, 22 Aug 2024 11:58:40 GMT"

    );

    "Strict-Transport-Security" =     (

        "max-age=15724800; includeSubDomains"

    );

    "x-envoy-upstream-service-time" =     (

        23

    );

} }

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
{count} votes

1 answer

Sort by: Most helpful
  1. navba-MSFT 27,540 Reputation points Microsoft Employee Moderator
    2024-08-22T12:48:16.7733333+00:00

    @Bhagwan Rajput@BHAGWAN RAJPUT Welcome to Microsoft Q&A Forum, Thank you for posting your query here! .

    You might observe one of several authentication errors, depending on the programming environment, API, or SDK. Here are some example errors:

    • Did you set the speech resource key and region values?
    • AuthenticationFailure
    • HTTP 403 Forbidden or HTTP 401 Unauthorized. Connection requests without a valid Ocp-Apim-Subscription-Key or Authorization header are rejected with a status of 403 or 401.
    • ValueError: can't construct SpeechConfig with the given arguments (or a variation of this message). This error could be observed, for example, when you run one of the Speech SDK for Python quickstarts without setting valid credentials.
    • Exception with an error code: 0x5. This access denied error could be observed, for example, when you run one of the Speech SDK for C# quickstarts without setting valid credentials.

    For baseline authentication troubleshooting tips, see validate your resource key and validate an authorization token.

    .

    Hope this helps.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.