Reputation: 682
I'm currently trying to use a service account to access GoogleSheets API - the problem I am having is with my .json file.
Here is my code:
try
{
string[] scopes = new string[] { SheetsService.Scope.Spreadsheets, SheetsService.Scope.SpreadsheetsReadonly }; // Put your scopes here
var stream = new FileStream("my_application_secret.json", FileMode.Open, FileAccess.Read);
var credential = GoogleCredential.FromStream(stream);
credential = credential.CreateScoped(scopes);
SheetsService service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "myApplication",
});
return service;
}
catch (Exception ex)
{
Console.WriteLine("Create service account myApplicationServiceAccount failed : " + ex.Message);
throw new Exception("Create ServiceAccount Failed : ", ex);
}
This kicks off my error which reads:
Create service account myApplicationServiceAccount failed : Error deserializing JSON credential data.
But everything I can find online says that what I have above should work.
Is there something more I have to do with this .json file?
Upvotes: 5
Views: 6719
Reputation: 682
As it turns out - because I'm supporting older versions of .NET (4 and below) I am using a slightly older version of the Google Sheets API.
But the Newtonsoft.JSON package was out of date as well. Updated that and now it's working.
Upvotes: 7
Reputation: 13469
You may check on this related GitHub thread. Given workaround is:
install-package Microsoft.Bcl.Build
install-package Microsoft.Bcl.Async
install-package Microsoft.Net.Http
Also, you can follow this tutorial about JSON Serialization/Deserialization in C# and see if you miss something.
Upvotes: 1
Reputation: 17654
try deserializing the object using Newtonsoft.Json
library
http://www.newtonsoft.com/json
return Newtonsoft.Json.JsonConvert.DeserializeObject<SheetsService>(service);
but the error is in deserializing JSON credential data, so this might be helpful :
credential = Newtonsoft.Json.JsonConvert.DeserializeObject<credential.CreateScoped>(scopes);
Upvotes: 0