Forum Libraries, Components, and Plugins

Environment Variable Problem Google Cloud Vision API

Hi,

I'm working on a prototype for an app using Google Cloud Vision API. I followed their quick start setup guide and everything was fine: I was able to run successfully the "gcloud auth application-default print-access-token" command in the terminal.

Once I run this code in my Xamarin Forms app:

var client = ImageAnnotatorClient.Create();
var response = client.DetectText(scan);
foreach (var annotation in response)
{
       if (annotation.Description != null)
         Console.WriteLine(annotation.Description);
}

I got this Exception:

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 "Link that I can't post because I'm new to the forum" for more information.

Which is self-explanatory, however, I run printenv, env, echo $GOOGLE_APPLICATION_CREDENTIALS and all of them shows me the variable being there. Also I can run without a problem the "gcloud auth application-default print-access-token" command too, so I doubt I have failed to set the environment variable.

To solve that I added the following code before creating the client variable:

Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "/Users/myuser/Documents/AFolderIMade/TheFileIDownloadedThatHoldsTheCredentials.json");

And I got this new exception:

System.InvalidOperationException
Error reading credential file from location /Users/myuser/Documents/AFolderIMade/TheFileIDownloadedThatHoldsTheCredentials.json: Could not find a part of the path "/Users/myuser/Documents/AFolderIMade/TheFileIDownloadedThatHoldsTheCredentials.json".

Please check the value of the Environment Variable GOOGLE_APPLICATION_CREDENTIALS

Which made me think I just entered the path to the credentials wrong but again I checked and it was correct.

To this point I couldn't think of another solution nor I could find one by googling around. Does somebody have run into this issue and solved it or can somebody point what I might be doing wrong?

Some info that might be useful:

  • I'm using Visual Studio 2019 for Mac Version 8.3.3.8
  • I Updated the NuGet for Newtonsoft.Json to 12.0.3 (Last stable at the time of this post)
  • I'm Using Xamarin Forms Version 4.4.0.991477

By the way, this is my first post in the forum, I have been using Xamarin Forms for 8 months so far but this is the first time I needed to ask something so please let me know if I'm missing some guideline for posting or something in case I am.

Thanks 0/

Answers

  • DeffaDeffa Member
    edited March 1

    Hey,

    I've run into the exact same problem on Windows, Xamarin, Visual Studio.
    I tried using the Google Cloud Firestore API, but it seems the Google Cloud Library doesn't support Xamarin:

    googleapis .github.io/google-cloud-dotnet/docs/guides/platforms.html

    Using this plugin instead solved it for Firestore:
    github .com/f-miyu/Plugin.CloudFirestore

    It won't help with your Google Cloud Vision API, but maybe this sets you on the right path.
    (Sorry, I'm not allowed to post the full links)

  • Gelo_VGelo_V Member ✭✭

    Thanks for replying Deffa, I will check the plugin during this week.
    Since it was just a prototype to test an idea I went with azure's cognitive services which worked.

Sign In or Register to comment.