现在我在Visual Studio中使用Xamarin进行了Cloud Vision API(https://cloud.google.com/dotnet/) . 我正在制作一个Android应用程序,我可以't figure out how to set the environment variable for the Cloud API. Google'的网站说:
将环境变量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);
}
}
1 回答
根据Getting Started with Authentication,您可以使用
command prompt
进行设置[PATH]
是包含服务帐户密钥的JSON文件的文件路径 .您可以在Google Cloud Platform Console中获取服务帐户密钥 . 你可以按照这个tutorial来获得它 .