Home > other >  "Unexpected character encountered" with Newtonsoft.Json reading GCP auth file
"Unexpected character encountered" with Newtonsoft.Json reading GCP auth file

Time:03-26

I'm trying to get a local c# program that I have to connect to GCP (Bigquery). I have gotten a credentials json file from GCP which looks something like:

{
  "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": "[email protected]",
  "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/[email protected]"
}

My code looks something like:

  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"]}");
  }

However when I get to the line that tries to deserialize the jsonString it complains that

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path '', line 1, position 1.'

I'm assuming the json isn't malformed as it was downloaded directly from GCP and I've checked with linters that it's valid. What's the next thing I should be trying?

CodePudding user response:

you don' t need this code, it doesn' t create anything , except the error

  string jsonStringDeserialized = JsonConvert.DeserializeObject<string>(jsonString);
  Console.WriteLine(jsonStringDeserialized);

try just this

 string jsonString = File.ReadAllText(path2key);
  var credential = GoogleCredential.FromJson(jsonString);
 .... your code
  • Related