Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error deserializing JSON credential Data C# Google Sheets API

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?

like image 575
Hanny Avatar asked Dec 22 '16 15:12

Hanny


2 Answers

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.

like image 87
Hanny Avatar answered Oct 19 '22 20:10

Hanny


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.

like image 44
abielita Avatar answered Oct 19 '22 19:10

abielita