Transcription result returns Access denied due to invalid subscription key or wrong API endpoint?

Annie 0 Reputation points
2024-06-25T21:41:35.3366667+00:00

I'm trying to run a batch transcription based on the example here.

I have extracted my subscription key via the screenshot attached:

Screenshot 2024-06-25 at 2.33.56 PM

and am trying to run the code via:

import requests
import json

subscription_key = "<KEY VIA SCREENSHOT>" 
service_region = "westus"

endpoint = "https://westus.api.cognitive.microsoft.com/speechtotext/v3.1/transcriptions"

headers = {
    "Ocp-Apim-Subscription-Key": subscription_key,
    "Content-Type": "application/json"
}

body = {
  "contentUrls": [
        "https://crbn.us/hello.wav",
        "https://crbn.us/whatstheweatherlike.wav"
  ],
  "properties": {
    "diarizationEnabled": True,
    "wordLevelTimestampsEnabled": True,
    "displayFormWordLevelTimestampsEnabled": True,
    "channels": [
      0,
      1
    ],
    "punctuationMode": "DictatedAndAutomatic",
    "profanityFilterMode": "Masked",
    "diarization": {
      "speakers": {
        "minCount": 1,
        "maxCount": 3
      }
    }
  },
  "locale": "en-US",
  "displayName": "test"
}

response = requests.post(endpoint, headers=headers, data=json.dumps(body))

# Check the response
if response.status_code == 201: 
    print("Transcription request submitted successfully!")
    response_data = response.json()
    print("Full response data:")
    print(json.dumps(response_data, indent=2)) 
    transcription_id = response_data['self'].split('/')[-1]
    print(f"Transcription ID: {transcription_id}")
else:
    print(f"Failed to submit transcription request. Status code: {response.status_code}")
    try:
        print(response.json())
    except json.JSONDecodeError:
        print(response.text)

This seems to be able to connect to the API and returns a transcription ID, but clicking into the links generated gives:

{"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}}

I have tried regenerating the API keys and am using the same sample data as the example - what is wrong with my code?

Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,498 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,558 questions
{count} votes