No overload for method 'ImageAnnotatorClient.Create' takes 1 arguments

Viewed 1580

I am using Google.Cloud.Vision.V1, Version=2.0.0.0 and the following below code from Google Vision API specify JSON file

using Google.Apis.Auth.OAuth2;
using Google.Cloud.Vision.V1;
using Grpc.Auth;
using Grpc.Core;

        var credential = GoogleCredential.FromFile("VisionProject.json");
        var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
        var client = ImageAnnotatorClient.Create(channel);

But its shows me this error No overload for method 'ImageAnnotatorClient.Create' takes 1 arguments.

I have found similar code in documentation https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Vision.V1P2Beta1/api/Google.Cloud.Vision.V1P2Beta1.ImageAnnotatorClient.html

But for some reason, it's not working( unable to see the overload)

3 Answers

It seems that you are using newer version of API. Docs state that now authentication is set up(when needed) via environment variable:

Otherwise, the simplest way of authenticating your API calls is to download a service account JSON file then set the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to it. The credentials will automatically be used to authenticate. See the Getting Started With Authentication guide for more details.

So you can do something like this:

 Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "PathTo_VisionProject.json");
 var client = ImageAnnotatorClient.Create();

Or set this environment variable some other way.

While setting the environment variable is certainly a simple way of specifying which service account file to use, it's not the only one. You can use the builder to specify the path very easily:

var client = new ImageAnnotatorClientBuilder
{
    CredentialsPath = "VisionProject.json"
}.Build();

Another solution if you do not have access to the JSON file directly and you want to stay close to what you previously did with the GoogleCredential and Channel creation is something like:

var credential = GoogleCredential.FromFile("VisionProject.json");
// or if you have access to the content only
// var credential = GoogleCredential.FromJson(json);
var client = await new ImageAnnotatorClientBuilder
{
    Endpoint = ImageAnnotatorClient.DefaultEndpoint, 
    ChannelCredentials = credential.ToChannelCredentials()
}.BuildAsync();
Related