I am trying to create a google api to extract text from image and later do a translation to English, my sample code bellow

 try
        {
            var JsonFilePath = @"C:\Users\shanjeeva\source\repos\TranslatorWindowsApp\Translator-fa8e022a7ba1.json";

            //Authenticate to the service by using Service Account
            var credential = GoogleCredential.FromFile(JsonFilePath).CreateScoped(ImageAnnotatorClient.DefaultScopes);
            var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());

            var client = ImageAnnotatorClient.Create(); // getting an error here

            var image = Image.FromFile(@"D:\Work Temp\Translate.JPG");
            // 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);
            }
        }
        catch (AnnotateImageException ex)
        {
            AnnotateImageResponse response = ex.Response;
            Console.WriteLine(response.Error);
        }
        catch (Exception ee)
        {
            Console.WriteLine(ee.Message);
            throw;
        }
    }

also I have setup google cloud application, generated necessary json file eg- Translator-XXXXX.json

some suggested to setup environment variable with the key "GOOGLE_APPLICATION_CREDENTIALS" also I tried, but am getting bellow error

'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.'

Can someone please help me?

1

There are 1 best solutions below

0
On

I was able to resolve with experts help, sample code bellow, added bellow code just before all other code and it works fine

        try
        {
            var GAC = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");

            if (GAC == null)
            {
                var path = @"D:\Work Temp\translator-298603-2bb5a20655a2.json";

                Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);
            }
        }
        catch (Exception)
        {
        }