I am following this tutorial to use Google Vision API, but even configuring the authentication credentials I get the following error:
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.'
My code in 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);
}
What can I do to fix this? Do I have to create a trial account to use the Google Cloud API?
These error messages are usually thrown when the application is not being authenticated correctly and can occur due to several reasons, such as missing files, invalid credential paths, incorrect environment variables assignations, among other causes. Keep in mind that when you set an environment variable value in a session, it is reset every time the session is dropped.
Based on this, I recommend you to validate that you have created the auth credential JSON file within your GCP project by following the 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 instructions:
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);
}
}
}
}
On the other hand, it is important to consider that the usage of the APIs require to have an active GCP account.
Finally, the "The name "StorageClient" does not exist in the current context)" error message is thrown as you are not adding the libraries and imports required to use the Cloud Storage and Languages services. Please note that these objects were added as an example of the authentication process; however, in case you want to use these features, I recommend you to take a look on the following Quickstarts Storage, Natural Language.