方法 'ImageAnnotatorClient.Create' 没有重载需要 1 个参数

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

我正在使用 Google.Cloud.Vision.V1, Version=2.0.0.0 以及来自

的以下代码
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);

但是它向我显示了这个错误 No overload for method 'ImageAnnotatorClient.Create' takes 1 arguments

我在文档中找到了类似的代码https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Vision.V1P2Beta1/api/Google.Cloud.Vision.V1P2Beta1.ImageAnnotatorClient.html

但由于某种原因,它不起作用(看不到过载)

看来您使用的是更新版本的 API。 Docs 声明现在通过环境变量设置身份验证(需要时):

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.

所以你可以这样做:

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

或者以其他方式设置此环境变量。

虽然设置环境变量当然是一种指定要使用哪个服务帐户文件的简单方法,但它并不是唯一的方法。您可以使用构建器非常轻松地指定路径:

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

另一种解决方案,如果您无法直接访问 JSON 文件,并且希望保持接近您之前对 GoogleCredentialChannel 创建所做的操作,则类似于:

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();