首页 > 解决方案 > Google Cloud Platform - Secret Manager 未格式化为 JSONObject 并给出错误

问题描述

我正在使用 Google Cloud Platform 开发 C# asp.net Web 应用程序。我正在使用存储 Google ClientSecret 和 ClientId 的 Secret Manager 功能。然后,我使用下面的代码,因为我的讲师使用它,我确信它可以工作,使用我的谷歌云项目名称、我的 secretId 和版本作为参数,使用密钥管理器功能获取客户端密钥和 ID。

public string GetGoogleClientId(string projectId, string SecretId, string version)
{
    SecretManagerServiceClient client = SecretManagerServiceClient.Create();

    SecretVersionName secretVersionName = new SecretVersionName(projectId, SecretId, version);

    AccessSecretVersionResponse result = client.AccessSecretVersion(secretVersionName);

    string payload = result.Payload.Data.ToStringUtf8().ToString();
    dynamic keys = JsonConvert.DeserializeObject(payload); //Gives the error here

    JObject jObject = JObject.Parse(payload);
    JToken jKey = jObject["Authentication:Google:ClientId"];
    return jKey.ToString();
}

但是,我收到以下错误:

'解析值时遇到意外字符:。路径'',第 0 行,位置 0。'

变量有效负载中生成的字符串返回我试图序列化的以下字符串:

{
  "Authentication:Google:ClientSecret": "Hidden for security reasons",
  "Authentication:Google:ClientId": "Hidden for security reasons"
}

请问我做错了什么?

标签: c#asp.netjson

解决方案


推荐阅读