问题描述
我该如何正确设置?描述看起来很简单
import os
import json
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
api_key='XYZ'
service_endpoint='https://api.eu-gb.speech-to-text.watson.cloud.ibm.com'
authenticator = IAMAuthenticator(api_key)
speech2text = SpeechToTextV1(authenticator=authenticator)
speech_to_text.set_service_url(service_endpoint)
speech_models = speech_to_text.list_models().get_result()
print(json.dumps(speech_models,indent=2))
我必须将哪个 API 密钥传递给“IAMAuthenticator”?
我不太明白是必须首先创建实例/资源 (https://cloud.ibm.com/catalog/services/speech-to-text) 并使用该 API 密钥,还是仅创建并使用 IBM Cloud API 密钥 (https://cloud.ibm.com/iam/apikeys)。我尝试了上述的不同组合,但无法成功连接,引发 ApiException: Error: Provided API key Could not be found,Code: 400
解决方法
实际上,您将 IAMAuthenticator
与 API 密钥一起使用。不要忘记导入它。
请参阅 API reference 中的示例以及 README 中有关 SDK 的更多信息。您可以使用相同的 API 密钥和 URL 使用 curl 进行调用吗
(BasicAuthenticator
专门用于用户名和密码验证。)
要直接使用 APIKEY,您必须使用 BasicAuthenticator
而不是 IAMAuthenticator
中的 ibm_cloud_sdk_core.authenticators
。请参阅下面的代码片段:
from ibm_watson import SpeechToTextV1,ApiException
from ibm_cloud_sdk_core.authenticators import BasicAuthenticator
...
# Basic Authentication with Watson STT API
stt_authenticator = BasicAuthenticator(
'apikey','<insert_your_watson_stt_apikey_here>'
)
# Construct a Watson STT client with the authentication object
stt = SpeechToTextV1(
authenticator=stt_authenticator
)
# Set the URL endpoint for your Watson STT client
stt.set_service_url(
'<insert_your_watson_stt_endpoint_url_here>'
)
# Read audio file and call Watson STT API:
with open(
os.path.join(
os.path.dirname(__file__),'./.','audio_sample.flac'
),'rb'
) as audio_file:
# Transcribe the audio.flac with Watson STT
# Recognize method API reference:
# https://cloud.ibm.com/apidocs/speech-to-text?code=python#recognize
stt_result = stt.recognize(
audio=audio_file,content_type='audio/flac',model='pt-BR_BroadbandModel'
).get_result()
# Print STT API call results
print(json.dumps(stt_result,indent=2))
...
完整示例位于:https://github.com/vnderlev/serverless_audio_processing/blob/master/action/action.py
希望这个回答对您有所帮助。