C#应用程序默认凭据不可用

时间:2019-05-15 14:59:03

标签: c# asp.net google-cloud-platform

我当前正在尝试访问云API,并且收到此错误:

  

应用程序默认凭据不可用。如果它们在Google Compute Engine中运行,则可用。否则,必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS指向指向定义凭据的文件。

我已经进入我的Google Cloud Platform帐户,启用了计费功能,创建了一个服务帐户,下载了一个JSON密钥,并在我的环境变量中设置了GOOGLE_APPLICATION_CREDENTIALS,并检查了它是否指向正确的JSON密钥。它指向我的API密钥 'C:\ Users \ jade.wilson \ source \ repos \ CloudApi \ CloudApi \ Services \ Keys \ CloudAPIKey.json'

这是代码:

   var credential = GoogleCredential.GetApplicationDefault();
   var channel = new Grpc.Core.Channel(SpeechClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());

   var client = SpeechClient.Create(channel);

   response = await client.RecognizeAsync(new RecognitionConfig()
            {
                Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
                SampleRateHertz = 16000,
                LanguageCode = "en",
            }, RecognitionAudio.FromStream(audio));

它没有超过第一行。还有其他我想念的东西吗?

1 个答案:

答案 0 :(得分:1)

如果您使用的是Visual Studio,只需关闭然后重新打开即可。

似乎,如果在打开Visual Studio的同时创建/更改环境变量,则将运行/调试的程序将无法看到它们。您必须退出Visual Studio并重新输入(对我来说:VS 2019版本16.7.2,Win 10版本2004-操作系统内部版本19041.450)