如何在 C# 中从 Google Cloud Vision 设置 GOOGLE_APPLICATION_CREDENTIALS?
How to set the GOOGLE_APPLICATION_CREDENTIALS from Google Cloud Vision in C#?
我正在关注 this tutorial 使用 Google Vision API,但即使配置身份验证凭据我也会收到以下错误:
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.'
我在 Visual Studio 2017 年的代码:
// Instantiates a client
var client = ImageAnnotatorClient.Create();
// Load the image file into memory
var image = Google.Cloud.Vision.V1.Image.FromFile(@"C:\Users\Maicon\OneDrive\Área de Trabalho\keyboardSantander\keyboard.png");
// Performs label detection on the image file
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
debugOutput(annotation.Description);
}
我该怎么做才能解决这个问题?我是否必须创建试用帐户才能使用 Google Cloud API?
这些错误消息通常在应用程序未被正确身份验证时抛出,并且可能由于多种原因而发生,例如丢失文件、无效的凭据路径、不正确的环境变量分配,除其他原因外。请记住,当您在会话中设置环境变量值时,每次会话被删除时它都会被重置。
基于此,我建议您按照 official documentation and that the file path is being correctly assigned. You can take a look on the code listed below which contains an example of the process required to authenticate your Vision API request in C# based on the Setting Up Authentication 说明验证您是否已在 GCP 项目中创建 auth 凭据 JSON 文件 :
using Google.Cloud.Vision.V1;
using System;
using Grpc.Auth;
using Google.Apis.Auth.OAuth2;
namespace VisionDemo
{
class Program
{
static void Main(string[] args)
{
//Authenticate to the service by using Service Account
var credential = GoogleCredential.FromFile(@"<CRED_JSON_FILEPATH>").CreateScoped(ImageAnnotatorClient.DefaultScopes);
var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
// Instantiates a client
var client = ImageAnnotatorClient.Create(channel);
var image = Image.FromFile(@"<IMAGE_FILE_PATH>");
// Performs label detection on the image file
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
Console.WriteLine(annotation.Description);
}
}
}
}
另一方面,重要的是要考虑到 API 的使用需要有一个有效的 GCP 帐户。
最后,"The name "StorageClient“在当前上下文中不存在)”错误消息被抛出,因为您没有添加使用 Cloud Storage 和 语言服务 。请注意,这些对象是作为身份验证过程的示例添加的;但是,如果您想使用这些功能,我建议您查看以下快速入门 Storage, Natural Language.
以上方法我都试过了,还是不行。对我有用的是通过环境 class.
中的 SetEnvironmentVariable 添加环境变量
string credential_path = @"PATH TO GOOGLE AUTH CREDENTIAL JSON FILE";
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);
var client = ImageAnnotatorClient.Create();
var image = Image.FromFile(@"PATH TO IMAGE");
var response = client.DetectDocumentText(image);
我正在关注 this tutorial 使用 Google Vision API,但即使配置身份验证凭据我也会收到以下错误:
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.'
我在 Visual Studio 2017 年的代码:
// Instantiates a client
var client = ImageAnnotatorClient.Create();
// Load the image file into memory
var image = Google.Cloud.Vision.V1.Image.FromFile(@"C:\Users\Maicon\OneDrive\Área de Trabalho\keyboardSantander\keyboard.png");
// Performs label detection on the image file
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
debugOutput(annotation.Description);
}
我该怎么做才能解决这个问题?我是否必须创建试用帐户才能使用 Google Cloud API?
这些错误消息通常在应用程序未被正确身份验证时抛出,并且可能由于多种原因而发生,例如丢失文件、无效的凭据路径、不正确的环境变量分配,除其他原因外。请记住,当您在会话中设置环境变量值时,每次会话被删除时它都会被重置。
基于此,我建议您按照 official documentation and that the file path is being correctly assigned. You can take a look on the code listed below which contains an example of the process required to authenticate your Vision API request in C# based on the Setting Up Authentication 说明验证您是否已在 GCP 项目中创建 auth 凭据 JSON 文件 :
using Google.Cloud.Vision.V1;
using System;
using Grpc.Auth;
using Google.Apis.Auth.OAuth2;
namespace VisionDemo
{
class Program
{
static void Main(string[] args)
{
//Authenticate to the service by using Service Account
var credential = GoogleCredential.FromFile(@"<CRED_JSON_FILEPATH>").CreateScoped(ImageAnnotatorClient.DefaultScopes);
var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
// Instantiates a client
var client = ImageAnnotatorClient.Create(channel);
var image = Image.FromFile(@"<IMAGE_FILE_PATH>");
// Performs label detection on the image file
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
Console.WriteLine(annotation.Description);
}
}
}
}
另一方面,重要的是要考虑到 API 的使用需要有一个有效的 GCP 帐户。
最后,"The name "StorageClient“在当前上下文中不存在)”错误消息被抛出,因为您没有添加使用 Cloud Storage 和 语言服务 。请注意,这些对象是作为身份验证过程的示例添加的;但是,如果您想使用这些功能,我建议您查看以下快速入门 Storage, Natural Language.
以上方法我都试过了,还是不行。对我有用的是通过环境 class.
中的 SetEnvironmentVariable 添加环境变量string credential_path = @"PATH TO GOOGLE AUTH CREDENTIAL JSON FILE";
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);
var client = ImageAnnotatorClient.Create();
var image = Image.FromFile(@"PATH TO IMAGE");
var response = client.DetectDocumentText(image);