【问题标题】:Google cloud vision api-authentication exception谷歌云视觉api-authentication异常
【发布时间】:2018-04-05 12:56:18
【问题描述】:

我正在尝试使用 google cloud vision api 文本检测。

using System;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Vision.V1;
using Google.Api.Gax.Grpc;
namespace blablabla
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"D:\Manisha\Pictures\1.png";

            var image = Image.FromFile(filePath);

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

我在
收到以下错误 "var client = ImageAnnotatorClient.Create();"

“应用程序默认凭据不可用。如果在 Google Compute Engine 中运行,它们可用。否则,必须定义环境变量 GOOGLE_APPLICATION_CREDENTIALS 指向定义凭据的文件。有关详细信息,请参阅https://developers.google.com/accounts/docs/application-default-credentials。”

我已将 GOOGLE_APPLICATION_CREDENTIALS 设置为 json 文件路径。 我到底哪里错了。我是否遗漏了一些重要步骤?

【问题讨论】:

  • GOOGLE_APPLICATION_CREDENTIALS 的值是否正确?您的应用程序可以访问该环境变量吗?您的应用程序/Visual Studio 是否需要重新启动?凭证的价值是什么? var credential = GoogleCredential.GetApplicationDefault();
  • 重启 Visual Studio 有帮助。谢谢。那行“var credential = GoogleCredential.GetApplicationDefault();”是什么?我应该添加它。我上面的代码错了吗?
  • 这似乎是一种获取凭据实例的方法,以防您想对其进行测试。 ImageAnnotatorClient 不需要。
  • @DanWilson 可以将您的评论变成答案吗?

标签: c# authentication ocr google-cloud-vision


【解决方案1】:

在这种情况下,您可以检查几件事。

  • GOOGLE_APPLICATION_CREDENTIALS 的值是否正确?
  • 您的应用程序可以访问该环境变量吗?
  • 您的应用/Visual Studio 是否需要重新启动?
  • 凭证的价值是什么?

检查 SDK 正在使用的凭据的一种方法如下。

var credential = GoogleCredential.GetApplicationDefault();

这似乎是一种获取凭据实例的方法,以防您想对其进行测试。 ImageAnnotatorClient 本身没有必要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    相关资源
    最近更新 更多