首页 > 解决方案 > Google Cloud Vision API - 创建 Grpc.Core.Channel 时出错

问题描述

我正在尝试使用 Google Cloud Vision V1 Api 的 ImageAnnotatorClient 类。我正在关注https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Vision.V1/api/Google.Cloud.Vision.V1.ImageAnnotatorClient.html下的示例

创建(ServiceEndpoint,ImageAnnotatorSettings)

标题。我正在使用 C# 并尝试构建一个经典的控制台应用程序。我正在使用来自 Nuget 的 GRPC.Core 版本 1.15.0 和 Google.Cloud.Vision.V1 版本 1.2.0。我得到一个编译错误

“GoogleCredential”不包含“ToChannelCredentials”的定义,并且找不到接受“GoogleCredential”类型的第一个参数的扩展方法“ToChannelCredentials”

以下是我的代码:

GoogleCredential credential = GoogleCredential
    .FromFile(@"C:\Users\...\12345.json")
    .CreateScoped(ImageAnnotatorClient.DefaultScopes);
            Google.Cloud.Vision.V1.Image image1 = Google.Cloud.Vision.V1.Image.FromFile(@"c:\Users\....\Image14b.png");

            Channel channel = new Channel(
    ImageAnnotatorClient.DefaultEndpoint.Host, ImageAnnotatorClient.DefaultEndpoint.Port, credential.ToChannelCredentials());
            ImageAnnotatorClient client = ImageAnnotatorClient.Create(channel);

            IReadOnlyList<EntityAnnotation> textAnnotations = client.DetectText(image1);

我在下面的行中收到错误:

        Channel channel = new Channel(
ImageAnnotatorClient.DefaultEndpoint.Host, ImageAnnotatorClient.DefaultEndpoint.Port, credential.ToChannelCredentials());

请问有什么提示吗?

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

解决方案


您可能缺少使用指令之一,特别是

using Grpc.Auth;

请参阅此处ToChannelCredentials作为扩展方法的定义。

检查您是否也包含using了示例中存在的所有其他指令。


推荐阅读