How to Authenticate to Watson IBM.WatsonDeveloperCloud.SpeechToText.v1 .NET API objSpeechToText.SetCredential(objTokenOptions) Fails (Unauthorized)

0

OVERVIEW

I am running Visual Studio 2017 15.9.3 and Microsoft.NET Framework 4.7.03190

I installed all Visual Studio Updates before installing the API from Nuget

I have installed theIBM.WatsonDeveloperCloud.SpeechToText.v1 Version 2.11.0 Nuget Package in Visual Studio 2017.

My Project is using the 4.7 Framework and the installation was totally sucessful.

I am trying to ultimately create a speech to text converter in Visual Basic (Not C#) the will translate speech to text in real-time.

I have been searching everywhere for coding examples and even contacted IBM Support and the only example they could point me to was python and java.

So with nothing else left I explored the API in Visual Basic's Object Viewer in an effort to try to get started with at least something.

The project below will successfully record to file NAudio and what I intend is to grab the audio recording buffer to sent as a stream to Watson. Don't know if that approach will work or not and I will cross that bridge later, right now I need to be able to authenticate first.

QUESTION

Can anyone help me figure out how to authenticate using the SetCredential method passing it the apikey and the URL https://stream.watsonplatform.net/speech-to-text/api/v1/recognize? It does not support username and password you must use apikey and URL.

VB CODE

Imports IBM.WatsonDeveloperCloud.Util
Imports IBM.WatsonDeveloperCloud.SpeechToText.v1
Imports NAudio.Wave
Imports System.ComponentModel

Public Class Form1
    Dim WithEvents objWaveInEvent As WaveInEvent = New WaveInEvent()
    Dim WithEvents objWaveFileWriter As WaveFileWriter = Nothing
    Dim blnClosing As Boolean = False
    Dim outputFilePath As String = Application.StartupPath & "/outputfile.wav"
    Dim outputFilePath2 As String = Application.StartupPath & "/outputfile2.wav"
    Dim objStream As IO.Stream
    Dim byteArrayAudio As Byte()
    Dim objSpeechToText As New SpeechToTextService
    Dim speechRecognitionResults As New Model.SpeechRecognitionResults

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Private Sub buttonRecord_Click(sender As Object, e As EventArgs) Handles buttonRecord.Click

        ' Start the Recording
        objWaveFileWriter = New WaveFileWriter(outputFilePath, objWaveInEvent.WaveFormat)
        objWaveInEvent.StartRecording()
        buttonRecord.Enabled = False
        buttonStop.Enabled = True

        ' Authenticate to Watson
        Dim objTokenOptions As TokenOptions = New TokenOptions With {
                .IamApiKey = "<apikey>",
                .IamUrl = "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize"
            }
        Try
            objSpeechToText.SetCredential(objTokenOptions) '(should fail as "Unauthorized but does not")
        Catch exServiceResponse As IBM.WatsonDeveloperCloud.Http.Exceptions.ServiceResponseException
            Debug.WriteLine("objSpeechToText.SetCredential(objTokenOptions): " & exServiceResponse.Message)
        End Try

    End Sub

    Private Sub objWaveInEvent_DataAvailable(sender As Object, e As WaveInEventArgs) Handles objWaveInEvent.DataAvailable
        objWaveFileWriter.Write(e.Buffer, 0, e.BytesRecorded)

        objStream = New IO.MemoryStream(e.Buffer)

        'This next line fails with the following error from Watson:
        'IBM.WatsonDeveloperCloud.Http.Exceptions.ServiceResponseException
        '  HResult=0x80131500
        '  Message=The API query failed with status code Unauthorized: Unauthorized | x-global-transaction-id:  | X-DP-Watson-Tran-ID: 
        '  Source=IBM.WatsonDeveloperCloud.SpeechToText.v1
        Try
            objSpeechToText.Recognize("audio/l16; rate=1600", objStream, "chunked")
        Catch exServiceResponse As IBM.WatsonDeveloperCloud.Http.Exceptions.ServiceResponseException
            Debug.WriteLine("objSpeechToText.Recognize(): " & exServiceResponse.Message)
        End Try

        If (objWaveFileWriter.Position > objWaveInEvent.WaveFormat.AverageBytesPerSecond * 30) Then
            objWaveInEvent.StopRecording()
        End If

    End Sub

    Private Sub objWaveInEvent_RecordingStopped(sender As Object, e As StoppedEventArgs) Handles objWaveInEvent.RecordingStopped
        objWaveFileWriter.Dispose()
        objWaveFileWriter = Nothing
        buttonRecord.Enabled = True
        buttonStop.Enabled = False
        If (blnClosing) Then
            objWaveInEvent.Dispose()
        End If
    End Sub

    Private Sub buttonStop_Click(sender As Object, e As EventArgs) Handles buttonStop.Click
        objWaveInEvent.StopRecording()
    End Sub

    Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        blnClosing = True
        objWaveInEvent.StopRecording()
    End Sub

End Class

PHP CODE (This Works)

I have also included some PHP code for proof of concept that works great with the same apikey and URL but it reads a flac file from my web server. You will have to create a flac audio test file (test.flac).

<?php
  $url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize';
  $file = fopen('test.flac', 'r');
  $size = filesize('test.flac');
  $fildata = fread($file,$size);
  $headers = array(    "Content-Type: audio/flac",
            "Transfer-Encoding: chunked");

  //apikey:snWVDdTG-lK2xkqdk7or9uf0mVkiwhP2o3E9TbO_z3kQ                     
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  //curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  curl_setopt($ch, CURLOPT_USERPWD, "apikey:<apikey>"); //PUT YOUR API KEY IN PLACE OF <apikey>
  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $fildata);
  curl_setopt($ch, CURLOPT_INFILE, $file);
  curl_setopt($ch, CURLOPT_INFILESIZE, $size);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $executed = curl_exec($ch);
  curl_close($ch);
  //var_dump($executed);
  $obj = json_decode($executed,true);
  echo $obj["results"][0]["alternatives"][0]["confidence"] . '~' . $obj["results"][0]["alternatives"][0]["transcript"];
?>
vb.net
authentication
ibm-cloud
ibm-watson
speech-to-text
asked on Stack Overflow Dec 6, 2018 by Larry Robertson • edited Dec 6, 2018 by Larry Robertson

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0