请求时 401 未经授权 Bing 文字转语音 API
401 Unauthorized when requesting Bing Text to Speech API
我正在使用 Azure-Samples github 中的这个示例。
https://github.com/Azure-Samples/Cognitive-Speech-TTS/blob/master/Samples-Http/Python/TTSSample.py
我用自己的密钥替换了 api 密钥,但出现 401 错误。
这是来自 API 的响应代码和回复。
401 Unauthorized
Response text: b''
以下是我使用的路径:
token_host = "api.cognitive.microsoft.com"
token_path = "/sts/v1.0/issueToken"
speech_host = "westus.tts.speech.microsoft.com"
speech_path = "/cognitiveservices/v1"
知道发生了什么事吗?
我遇到了类似的问题,看来正确的 API 不是
https://westus.tts.speech.microsoft.com/cognitiveservices/v1
但是
我遇到了同样的 401 问题。我切换了区域,尝试了各种端点都无济于事。
使用此代码可以正常工作:
import requests
key = "< YOUR API KEY GOES HERE >"
headers = {
'Content-type': 'application/x-www-form-urlencoded',
'Content-Length': '0',
'Ocp-Apim-Subscription-Key': key,
}
res = requests.post('https://westus.api.cognitive.microsoft.com/sts/v1.0/issuetoken', headers=headers)
print(res.status_code)
token = res.content.decode("utf-8")
fileformat = "audio-16khz-128kbitrate-mono-mp3"
lang = "en-US"
gender = "Male"
voice = "Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)"
text = "This is a sample text, that Benjamin will speak."
headers = { "Content-type" : "application/ssml+xml",
"X-Microsoft-OutputFormat" : fileformat,
"User-Agent" : "TTSForPython",
"Authorization" : "Bearer " + token,
}
body = f"<speak version='1.0' xml:lang='{lang}'><voice xml:lang='{lang}' xml:gender='{gender}' name='{voice}'>{text}</voice></speak>"
res = requests.post('https://westus.tts.speech.microsoft.com/cognitiveservices/v1', headers=headers, data=body)
print(res.status_code)
audiofile = res.content
with open("file.mp3", "wb") as f:
f.write(audiofile)
我正在使用 Azure-Samples github 中的这个示例。 https://github.com/Azure-Samples/Cognitive-Speech-TTS/blob/master/Samples-Http/Python/TTSSample.py
我用自己的密钥替换了 api 密钥,但出现 401 错误。
这是来自 API 的响应代码和回复。
401 Unauthorized
Response text: b''
以下是我使用的路径:
token_host = "api.cognitive.microsoft.com"
token_path = "/sts/v1.0/issueToken"
speech_host = "westus.tts.speech.microsoft.com"
speech_path = "/cognitiveservices/v1"
知道发生了什么事吗?
我遇到了类似的问题,看来正确的 API 不是
https://westus.tts.speech.microsoft.com/cognitiveservices/v1
但是
我遇到了同样的 401 问题。我切换了区域,尝试了各种端点都无济于事。
使用此代码可以正常工作:
import requests
key = "< YOUR API KEY GOES HERE >"
headers = {
'Content-type': 'application/x-www-form-urlencoded',
'Content-Length': '0',
'Ocp-Apim-Subscription-Key': key,
}
res = requests.post('https://westus.api.cognitive.microsoft.com/sts/v1.0/issuetoken', headers=headers)
print(res.status_code)
token = res.content.decode("utf-8")
fileformat = "audio-16khz-128kbitrate-mono-mp3"
lang = "en-US"
gender = "Male"
voice = "Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)"
text = "This is a sample text, that Benjamin will speak."
headers = { "Content-type" : "application/ssml+xml",
"X-Microsoft-OutputFormat" : fileformat,
"User-Agent" : "TTSForPython",
"Authorization" : "Bearer " + token,
}
body = f"<speak version='1.0' xml:lang='{lang}'><voice xml:lang='{lang}' xml:gender='{gender}' name='{voice}'>{text}</voice></speak>"
res = requests.post('https://westus.tts.speech.microsoft.com/cognitiveservices/v1', headers=headers, data=body)
print(res.status_code)
audiofile = res.content
with open("file.mp3", "wb") as f:
f.write(audiofile)