现在我正在Visual Studio中使用Google的Cloud Vision API(https://cloud.google.com/dotnet/(和Xamarin。 我正在努力使用它制作一个安卓应用程序,但我无法弄清楚如何为云 API 设置环境变量。 谷歌的网站说:
将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为包含服务帐号密钥的 JSON 文件的文件路径。
我不确定该怎么做。 我在下面有我的代码。 当我运行它时,我收到此错误:
未处理的异常:
System.InvalidOperationException: 应用程序默认凭据不可用。如果在 Google Compute Engine 中运行,它们可用。否则,必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS,指向定义凭据的文件。有关详细信息,请参阅 https://developers.google.com/accounts/docs/application-default-credentials。发生
我是 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);
}
}
我不知道如何为云 API 设置环境变量。
根据身份验证入门,可以使用command prompt
set GOOGLE_APPLICATION_CREDENTIALS=[PATH]
[PATH]
是包含服务帐号密钥的 JSON 文件的文件路径。
您可以在 Google Cloud Platform Console 中获取服务帐号密钥。 您可以按照本教程获取它。