Alex Choroshin
Alex Choroshin

Reputation: 6187

Using google speech API in C# returns 403 error

Trying to use Google Speech API in C# returns 403.

In Google Cloud Platform I generated a key and still getting the 403 error.

Used this code:

class Program
 {
    static void Main(string[] args)
    {
        try
        {

            FileStream fileStream = File.OpenRead("good-morning-google.flac");
            MemoryStream memoryStream = new MemoryStream();
            memoryStream.SetLength(fileStream.Length);
            fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
            byte[] BA_AudioFile = memoryStream.GetBuffer();
            HttpWebRequest _HWR_SpeechToText = null;
            _HWR_SpeechToText =
                        (HttpWebRequest)HttpWebRequest.Create(
                            "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=YOUR_API_KEY_HERE");
            _HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
            _HWR_SpeechToText.Method = "POST";
            _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
            _HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
            Stream stream = _HWR_SpeechToText.GetRequestStream();
            stream.Write(BA_AudioFile, 0, BA_AudioFile.Length);
            stream.Close();

            HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
            if (HWR_Response.StatusCode == HttpStatusCode.OK)
            {
                StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
                Console.WriteLine(SR_Response.ReadToEnd());
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        Console.ReadLine();
    }
}

It's probably some invalid key issue,tried generating server key and browser key, same result, 403 (forbidden)

please help.

Upvotes: 0

Views: 2096

Answers (3)

JMA
JMA

Reputation: 1825

This doesn't seem to work anymore. The speech API doesn't show up.

Upvotes: 1

Ravi A.
Ravi A.

Reputation: 2213

Was getting the same 403 error and below is what helped me to fix

Step 1 - Used quick start speech API documentation - https://cloud.google.com/speech/docs/getting-started and found billing was disabled. Below is the detailed error message that I have received as curl output i.e. 3rd step in the document

{
  "error": {
    "code": 403,
    "message": "Project xxxxx (#xxxxxx) has billing disabled. Please enable it.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developer console API key",
            "url": "https://console.developers.google.com/project/1026744225026/apiui/credential"
          }
        ]
      }
    ]
  }
}

Enabled billing for the project.

Step 2 - Make sure you are a member of [email protected] i.e. step 1 as per http://www.chromium.org/developers/how-tos/api-keys (Also refer https://github.com/gillesdemey/google-speech-v2/issues/8)

Once you are member Go to your Project - > click Enable API -> Search for speech API it will have 2 results - > Enable 'Speech API Private API' (before subscribing to group you would only see 'Google Cloud Speech API' now you can see 'Speech API Private API')

Hope this helps

Upvotes: 0

Jordan Masters
Jordan Masters

Reputation: 1

I haven't tried using the http API but here's my working code using the Google Speech API Library (Google.Apis.CloudSpeechAPI.v1beta1):

void Main()
{
    //create the service and auth
    CloudSpeechAPIService service = new CloudSpeechAPIService(new BaseClientService.Initializer
    {
        ApplicationName = "Speech API Test",
        ApiKey = "your API key..."
    });

    //configure the audio file properties
    RecognitionConfig sConfig = new RecognitionConfig();
    sConfig.Encoding = "FLAC";
    sConfig.SampleRate = 16000;
    sConfig.LanguageCode = "en-AU";

    //make the request and output the transcribed text to console
    SyncRecognizeResponse response = getResponse(service, sConfig, "audio file.flac");
    string resultText = response.Results.ElementAt(0).Alternatives.ElementAt(0).Transcript;
    Console.WriteLine(resultText);
}


public SyncRecognizeResponse getResponse(CloudSpeechAPIService sService, RecognitionConfig sConfig, string fileName)
{
    //read the audio file into a base 64 string and add to API object
    RecognitionAudio sAudio = new RecognitionAudio();
    byte[] audioBytes = getAudioStreamBytes(fileName);
    sAudio.Content = Convert.ToBase64String(audioBytes);

    //create the request with the config and audio files
    SyncRecognizeRequest sRequest = new SyncRecognizeRequest();
    sRequest.Config = sConfig;
    sRequest.Audio = sAudio;

    //execute the request
    SyncRecognizeResponse response = sService.Speech.Syncrecognize(sRequest).Execute();

    return response;
}


public byte[] getAudioStreamBytes(string fileName)
{
    FileStream fileStream = File.OpenRead(fileName);
    MemoryStream memoryStream = new MemoryStream();
    memoryStream.SetLength(fileStream.Length);
    fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
    byte[] BA_AudioFile = memoryStream.GetBuffer();
    return BA_AudioFile;
}

Upvotes: 0

Related Questions