Python 401请求Bing文本到语音API时未经授权

Python 401请求Bing文本到语音API时未经授权,python,text-to-speech,bing-api,Python,Text To Speech,Bing Api,我正在使用来自Azure示例github的这个示例。 我用自己的api密钥替换了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.co

我正在使用来自Azure示例github的这个示例。

我用自己的api密钥替换了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

但是


我也有同样的问题。我切换了区域,尝试了不同的端点,但没有结果

让它与此代码一起工作:

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)
导入请求
key=“<您的API密钥在此处>”
标题={
“内容类型”:“应用程序/x-www-form-urlencoded”,
“内容长度”:“0”,
“Ocp Apim订阅密钥”:密钥,
}
res=requests.post('https://westus.api.cognitive.microsoft.com/sts/v1.0/issuetoken,headers=headers)
打印(res.status_代码)
令牌=资源内容解码(“utf-8”)
fileformat=“audio-16khz-128kbitrate-mono-mp3”
lang=“en US”
性别=“男性”
voice=“Microsoft服务器语音文本到语音语音(en-US,BenjaminRUS)”
text=“这是Benjamin将要讲的示例文本。”
headers={“内容类型”:“应用程序/ssml+xml”,
“X-Microsoft-OutputFormat”:文件格式,
“用户代理”:“TTSForPython”,
“授权”:“持票人”+代币,
}
body=f“{text}”
res=requests.post('https://westus.tts.speech.microsoft.com/cognitiveservices/v1,headers=headers,data=body)
打印(res.status_代码)
audiofile=res.content
打开(“file.mp3”、“wb”)作为f:
f、 写入(音频文件)

嗨!你解决了这个问题吗?我在环境方面也遇到了同样的问题,这在以前很正常。@zou chu也有同样的问题。看我的答案。这很有趣。我会试试这个,我已经有一个星期的问题了。在我的情况下正好相反。我只能使用
https://westus.tts.speech.microsoft.com/cognitiveservices/v1
而不是与
https://speech.platform.bing.com/synthesize
尽管API文档中的说明不同。