2016-08-25 112 views
0

尝试使用谷歌语音API在C#返回403使用谷歌语音API返回403错误

在谷歌云平台我生成密钥,并仍然得到403错误。

使用此代码:

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(); 
    } 
} 

这也可能是一些无效的关键问题,试图生成服务器密钥和浏览器键,同样的结果,403(禁止)

请帮助。

+0

您是否尝试过使用客户端库,而不是直接使用'HttpWebRequest'进行调用? (试图找到正确的版本 - https://www.nuget.org/packages/Google.Apis.CloudSpeechAPI.v1beta1/适用于v1beta1 ...这可能是我们正在寻找不同的API。) –

+0

谢谢@JonSkeet,我会试试看。 –

+0

@JonSkeet,你有一个使用Google.Apis.CloudSpeechAPI.v1beta1的工作示例吗?或甚至使用这个库的一些例子?我想使用一个密钥而不是oauth2 –

回答

0

我没有使用HTTP API尝试,但这里是使用谷歌语音API库(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; 
} 
0

渐渐同403错误及以下什么帮助我修复

第1步 - 使用快速启动语音API文档 - https://cloud.google.com/speech/docs/getting-started并且发现帐单被禁用。下面是我的文档中接收作为卷曲输出即第三步骤

{ 
    "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" 
      } 
     ] 
     } 
    ] 
    } 
} 

启用计费项目的详细的错误消息。

第2步 - 确保您[email protected]的成员,即第1步按http://www.chromium.org/developers/how-tos/api-keys(另请参见https://github.com/gillesdemey/google-speech-v2/issues/8

一旦您是成员转到您的项目 - >点击启用API - >搜索语音API它将有2个结果 - >启用'语音API私人API'(在订阅组之前您只能看到'Google Cloud Speech API',现在您可以看到'语音API私有API')

Hope这有助于

0

这似乎不工作了。语音API不显示。