"Unexpected character encountered" Newtonsoft.Json 读取 GCP 授权文件
"Unexpected character encountered" with Newtonsoft.Json reading GCP auth file
我正在尝试获取必须连接到 GCP (Bigquery) 的本地 c# 程序。
我从 GCP 获得了一个凭证 json 文件,它看起来像:
{
"type": "service_account",
"project_id": "project-id-1",
"private_key_id": "veryprivatekeything",
"private_key": "-----BEGIN PRIVATE KEY-----\nmany_letters_and_symbols_here\n-----END PRIVATE KEY-----\n",
"client_email": "bq-access-for-test@project-id-1.iam.gserviceaccount.com",
"client_id": "1234567891011",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/bq-access-for-test%40project-id-1.iam.gserviceaccount.com"
}
我的代码类似于:
string path2key = "C:/Users/me/Downloads/project-id-1-letters.json";
byte[] jsonBytes = File.ReadAllBytes(path2key);
// I also tried another encoding but same deal
// string jsonString = Encoding.UTF32.GetString(jsonBytes);
string jsonString = File.ReadAllText(path2key);
Console.WriteLine(jsonString);
// This is where the error is
string jsonStringDeserialized = JsonConvert.DeserializeObject<string>(jsonString);
Console.WriteLine(jsonStringDeserialized);
// I presume I'm passing json as a string???
var credential = GoogleCredential.FromJson(jsonStringDeserialized);
if (credential.IsCreateScopedRequired)
{
credential = credential.CreateScoped(new[]
{
BigqueryService.Scope.Bigquery
});
}
Console.WriteLine("Credentials Created");
var client = BigQueryClient.Create("projectId", credential);
Console.WriteLine("Client Created");
var table = client.GetTable("bigquery-public-data", "samples", "shakespeare");
var sql =
$" SELECT corpus AS title" +
$" , COUNT(word) AS unique_words " +
$" FROM {table} " +
$" GROUP BY title " +
$" ORDER BY unique_words DESC " +
$" LIMIT 10"
;
var results = client.ExecuteQuery(sql, parameters: null);
foreach (var row in results)
{
Console.WriteLine($"{row["title"]}: {row["unique_words"]}");
}
然而,当我到达试图反序列化 jsonString 的行时,它抱怨说
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path '', line 1, position 1.'
我假设 json 没有格式错误,因为它是直接从 GCP 下载的,我已经用 linters 检查它是否有效。
接下来我应该尝试什么?
您不需要此代码,它不会创建任何东西,除了错误
string jsonStringDeserialized = JsonConvert.DeserializeObject<string>(jsonString);
Console.WriteLine(jsonStringDeserialized);
试试这个
string jsonString = File.ReadAllText(path2key);
var credential = GoogleCredential.FromJson(jsonString);
.... your code
我正在尝试获取必须连接到 GCP (Bigquery) 的本地 c# 程序。 我从 GCP 获得了一个凭证 json 文件,它看起来像:
{
"type": "service_account",
"project_id": "project-id-1",
"private_key_id": "veryprivatekeything",
"private_key": "-----BEGIN PRIVATE KEY-----\nmany_letters_and_symbols_here\n-----END PRIVATE KEY-----\n",
"client_email": "bq-access-for-test@project-id-1.iam.gserviceaccount.com",
"client_id": "1234567891011",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/bq-access-for-test%40project-id-1.iam.gserviceaccount.com"
}
我的代码类似于:
string path2key = "C:/Users/me/Downloads/project-id-1-letters.json";
byte[] jsonBytes = File.ReadAllBytes(path2key);
// I also tried another encoding but same deal
// string jsonString = Encoding.UTF32.GetString(jsonBytes);
string jsonString = File.ReadAllText(path2key);
Console.WriteLine(jsonString);
// This is where the error is
string jsonStringDeserialized = JsonConvert.DeserializeObject<string>(jsonString);
Console.WriteLine(jsonStringDeserialized);
// I presume I'm passing json as a string???
var credential = GoogleCredential.FromJson(jsonStringDeserialized);
if (credential.IsCreateScopedRequired)
{
credential = credential.CreateScoped(new[]
{
BigqueryService.Scope.Bigquery
});
}
Console.WriteLine("Credentials Created");
var client = BigQueryClient.Create("projectId", credential);
Console.WriteLine("Client Created");
var table = client.GetTable("bigquery-public-data", "samples", "shakespeare");
var sql =
$" SELECT corpus AS title" +
$" , COUNT(word) AS unique_words " +
$" FROM {table} " +
$" GROUP BY title " +
$" ORDER BY unique_words DESC " +
$" LIMIT 10"
;
var results = client.ExecuteQuery(sql, parameters: null);
foreach (var row in results)
{
Console.WriteLine($"{row["title"]}: {row["unique_words"]}");
}
然而,当我到达试图反序列化 jsonString 的行时,它抱怨说
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path '', line 1, position 1.'
我假设 json 没有格式错误,因为它是直接从 GCP 下载的,我已经用 linters 检查它是否有效。 接下来我应该尝试什么?
您不需要此代码,它不会创建任何东西,除了错误
string jsonStringDeserialized = JsonConvert.DeserializeObject<string>(jsonString);
Console.WriteLine(jsonStringDeserialized);
试试这个
string jsonString = File.ReadAllText(path2key);
var credential = GoogleCredential.FromJson(jsonString);
.... your code