c# - 收到错误 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:/Users/abcde/Documents/AndroidApp/My Project-d38b212eadaf.json
永远行不通。这是一个 Windows 文件系统。
此外,您必须从设备或模拟器开始推理。该设备不能只访问您的 Windows 文件系统并获取该文件。您必须以某种方式在您的设备上获取该文件,并使用Path
.NET 中的对象导航到正确的路径。甚至可能通过依赖服务来获得每个平台的正确路径。
那有意义吗?
推荐阅读
- python - 在 Python 中使用伪代码解释 Dijkstra 算法时遇到问题
- java - 如何重定向到 Cmis 文件夹?
- mongodb - 查询 MongoDB 数据库时忽略文本记录单词之间的字符子串
- angularjs - 如何清理光滑网格中的行数据?
- django - 在 django 应用程序中收集静态文件时发生 botocore ClientError
- typescript - 如何使用 CDK 为 CORS 正确配置 APIGateway
- node.js - 如何在 mongoose 聚合管道中的对象数组中填充 objectId?
- python - 如果缺少所需信息,锁定 tk 按钮
- python - 迭代 numpy 数组元素的最快方法是什么?
- rust - 如何使用 rust-lldb 在 coredump 中找到导致 SEGV 的线程?