目前,我正在 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/