首页 > 解决方案 > 收到错误 System.InvalidOperationException:反序列化 JSON 凭据数据时出错

问题描述

我正在尝试从 xamarin C# android 应用程序代码调用 google cloud vision api。我已经设置了环境变量,但我仍然无法调用 api。所以我决定通过传递凭证 json 文件来调用它,但现在我在反序列化 JSON 凭证数据时遇到错误

这是我的代码

string jsonPath = @"C:/Users/abcde/Documents/AndroidApp/My Project-d38b212eadaf.json";

            var credential = GoogleCredential.FromJson(jsonPath);
            var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
            var client = ImageAnnotatorClient.Create(channel);

            var image = Image.FromFile(@"C:/Users/abcde/Documents/AndroidApp/bg.jpg");

            var response = client.DetectLabels(image);

            foreach (var annotation in response)
            {
                if (annotation.Description != null)
                    Console.WriteLine(annotation.Description);
            }

标签: c#androidvisual-studioxamarin.formsxamarin.android

解决方案


像这样的道路C:/Users/abcde/Documents/AndroidApp/My Project-d38b212eadaf.json永远行不通。这是一个 Windows 文件系统。

此外,您必须从设备或模拟器开始推理。该设备不能只访问您的 Windows 文件系统并获取该文件。您必须以某种方式在您的设备上获取该文件,并使用Path.NET 中的对象导航到正确的路径。甚至可能通过依赖服务来获得每个平台的正确路径。

那有意义吗?


推荐阅读