我正在尝试对我的 Xamarin.Forms PCL-Project(便携式类库)中的 Google Cloud Speech Api 进行身份验证。我正在使用 Android 手机进行测试。我的解决方案的代码如下所示:
Assembly assembly = typeof(Program).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("ConsoleApp1.speech_auth.json");
string resultString = null;
using (StreamReader reader = new StreamReader(stream))
{
resultString = reader.ReadToEnd();
}
GoogleCredential credential = GoogleCredential.FromJson(resultString);
if (credential.IsCreateScopedRequired)
{
credential = credential.CreateScoped(new[] { "https://www.googleapis.com/auth/cloud-platform" });
}
var channel = new Grpc.Core.Channel(SpeechClient.DefaultEndpoint.Host,
credential.ToChannelCredentials());
var speech = SpeechClient.Create(channel);
var response = speech.Recognize(new RecognitionConfig()
{
Encoding = RecognitionConfig.Types.AudioEncoding.Flac,
SampleRateHertz = 16000,
LanguageCode = "de-CH",
}, RecognitionAudio.FromFile("test.flac"));
在一个通常的 .NET ConsoleApplication-Project 中,它的工作就像一个魅力。但是,如果我在我的 PCL 项目中尝试完全相同的代码,我会在以下行出现错误:
var channel = new Grpc.Core.Channel(SpeechClient.DefaultEndpoint.Host,
credential.ToChannelCredentials());
其中说:
未处理的异常:System.NotImplementedException:方法或操作未实现
我已经在我的 PCL和我的 Android 项目上安装了所有 Nuget GRPC 和 Google Speech API 包。
所以我的问题是:
是否有更好/更简单的方法来使用 Xamarin.Forms 对 Google Cloud Speech API 进行身份验证?如果没有,我的代码有什么问题?
编辑:似乎 Xamarin 不支持 gRPC。我已经设法使用简单的 httpClient 调用发送请求的语音 API。