首页 > 解决方案 > 方法“ImageAnnotatorClient.Create”没有重载需要 1 个参数

问题描述

我正在使用Google.Cloud.Vision.V1, Version=2.0.0.0以下来自Google Vision API 的代码指定 JSON 文件

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中找到了类似的代码

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

标签: c#google-cloud-platformgoogle-cloud-visiongoogle-client

解决方案


看来您正在使用更新版本的 API。文档状态现在通过环境变量设置身份验证(在需要时):

否则,验证 API 调用的最简单方法是下载服务帐户 JSON 文件,然后设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量以引用它。凭据将自动用于身份验证。有关更多详细信息,请参阅《身份验证入门》指南

所以你可以做这样的事情:

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

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


推荐阅读