c# - 在 Xamarin for Google Cloud API 中设置环境变量

标签 c# xamarin xamarin.android google-vision

目前,我正在 Visual Studio 中将 Google 的 Cloud Vision API ( https://cloud.google.com/dotnet/) 与 Xamarin 结合使用。我正在用它制作一个 android 应用程序,但我不知道如何为 Cloud API 设置环境变量。谷歌的网站说:

Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key.

我不太确定该怎么做。我的代码如下。当我运行它时,出现此错误:

Unhandled Exception:

System.InvalidOperationException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information. occurred

我是 C# 的新手,如果这是一个非常简单的修复,我很抱歉。非常感谢您!

public async void AnalyzePicAsync(object sender, EventArgs eventArgs)
{
  string json1 = "";
  //Gets API Credentials
  AssetManager assets = this.Assets;

  using (StreamReader sr = new StreamReader(assets.Open("computer-vision-test-204417-9d2666a5603a.json")))
  {
    json1 = sr.ReadToEnd();
  }
  //Instantiates a client

  GoogleCredential credential = GoogleCredential.FromJson(json1);

  var client = ImageAnnotatorClient.Create();
  // Load the image file into memory
  var image = Image.FromFile(_file.Path);
  // Performs label detection on the image file
  var response = client.DetectLabels(image);
  foreach (var annotation in response)
  {
    if (annotation.Description != null)
        System.Console.WriteLine(annotation.Description);
  }
}

最佳答案

I can't figure out how to set the environment variable for the Cloud API.

根据 Getting Started with Authentication ,你可以使用命令提示符

来设置它
set GOOGLE_APPLICATION_CREDENTIALS=[PATH]

[PATH] 是包含您的服务帐户 key 的 JSON 文件的文件路径。

您可以在 Google Cloud Platform Console 中获取服务帐户 key 。 你可以关注这个 tutorial得到它。

关于c# - 在 Xamarin for Google Cloud API 中设置环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50763328/

相关文章:

c# - 将 int 数组传递给 Web 服务

javascript - 使用 View 动态访问模型

xaml - Xamarin Forms 按钮内容布局不起作用

c# - 如何在MVVM Xamarin Forms中将参数传递给Singleton

sqlite - 使用MonoDroid/MonoTouch在移动应用程序中重用DataAccess

c# - Xamarin 5.0 创建启动画面

c# - 我应该将文件复制到多大的 block 中?

c# - 返回 IDisposable 对象后会发生什么情况?

c# - 使用 Objective Sharpie 将 Spotify iOS SDK 绑定(bind)到 C#

xamarin - 如何使用TabbedPage.ToolbarPlacement ="Bottom"-Xamarin Forms实现TabLayout.IOnTabSelectedListener.OnTabUnselected?