How can I access IBM speech-to-text api with curl?

删除回忆录丶 提交于 2019-12-06 08:40:13

The documentation for sessionless calls is available here.

Make sure you create an instance of the Watson Speech-to-Text services and use the credentials from that service (not your Bluemix user id and password).

You can create the service in the Bluemix UI and get the credentials, or you can create it via command line:

$ cf create-service speech_to_text standard my_service

And then you can create service credentials and get it:

$ cf create-service-key my_service credentials-1

$ cf service-key my_service credentials-1
Getting key credentials-1 for service instance my_service as adasilva@us.ibm.com...

{
 "password": "FJXUG6????",
 "url": "https://stream.watsonplatform.net/speech-to-text/api",
 "username": "147e438e-f633-4e40-8351-aaaaaaaaaa"
}

You can then use the username and password from above in your curl script. See curl command and results below (I masked username, password and other fields for security):

$ curl -X POST -v -u '80062070-1f88-4943-9a2a-aaaaaaa':'hwtTg???????' \
> --header 'Content-Type: audio/flac' \
> --data-binary @audio-file1.flac \
> 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?timestamps=true&continuous=true'
*   Trying 158.85.132.94...
* Connected to stream.watsonplatform.net (158.85.132.94) port 443 (#0)
* TLS 1.2 connection using TLS_RSA_WITH_AES_256_GCM_SHA384
* Server certificate: *.watsonplatform.net
* Server certificate: GeoTrust SSL CA - G3
* Server certificate: GeoTrust Global CA
* Server auth using Basic with user '80062070-1f88-4943-9a2a-aaaaaaa'
> POST /speech-to-text/api/v1/recognize?timestamps=true&continuous=true HTTP/1.1
> Host: stream.watsonplatform.net
> Authorization: Basic ODAwNjIwNzAtMWY4OC00OTQzLTlhMmEtYzE0OWVmOTJkM2IyOmh3dXXXXXX==
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: audio/flac
> Content-Length: 48363
> Expect: 100-continue
> 
< HTTP/1.1 100 Continue
< X-Note: Gateway Ack
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< X-Backside-Transport: OK OK
< Connection: Keep-Alive
< Transfer-Encoding: chunked
< Session-Name: LBBROMXYTJZKKEGE-en-US_BroadbandModel
< X-Content-Type-Options: nosniff
< Content-Disposition: inline; filename="result.json"
< X-XSS-Protection: 1
< Date: Sat, 08 Oct 2016 22:45:02 GMT
< Via: 1.1 c6a59f5, 1.1 022614b, HTTP/1.1 d941b9b
< Content-Type: application/json
< Server: -
< Set-Cookie: Watson-REMOVED; path=/speech-to-text/api; secure; HttpOnly
< X-Client-IP: 71.70.244.18
< X-Global-Transaction-ID: 775715492
< X-DP-Watson-Tran-ID: stream-dp02-775715492
< 
{
   "results": [
      {
         "alternatives": [
            {
               "timestamps": [
                  [
                     "the", 
                     0.03, 
                     0.09
                  ], 
                  [
                     "latest", 
                     0.09, 
                     0.6
                  ], 
                  [
                     "weather", 
                     0.6, 
                     0.85
                  ], 
                  [
                     "report", 
                     0.85, 
                     1.52
                  ]
               ], 
               "confidence": 0.98, 
               "transcript": "the latest weather report "
            }
         ], 
         "final": true
      }
   ], 
   "result_index": 0
* Connection #0 to host stream.watsonplatform.net left intact

I got the same problem. After many tries, I found that the URL is wrong. Remember the Service Credential info:

{
"url": "https://stream-fra.watsonplatform.net/text-to-speech/api",
"username": "xxxxx-xxxxx-xxxxx-xxxxx",
"password": "yyyyyyy"
}

Then, you must use the URL provided in that info. Following is good request for me:

E:/Curl/curl.exe -X GET -u "xxxxx-xxxxx-xxxxx-xxxxx":"yyyyyyy" --output hello_world.wav "https://stream-fra.watsonplatform.net/text-to-speech/api/v1/synthesize?accept=audio/wav&text=Hello%20world&voice=en-US_AllisonVoice"

My problem was that I had curly brackets around my credentials. The tutorial says:

curl -X POST -u {username}:{password}

so I put in my username and password but I left the curly brackets. Works much better without the curly brackets!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!