JSON descriptor deserialization error C # Data Google Sheets APIs

I am currently trying to use a service account to access the GoogleSheets API - the problem I encountered is related to 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 will write off my error, which reads:

Create service account myApplicationServiceAccount failed : Error deserializing JSON credential data.

But all I can find on the Internet says that what I have above should work.

Is there anything else I have to do with this .json file?

+4
source share
3 answers

As it turns out - because I support older versions of .NET (4 and below), I use a slightly older version of the Google Sheets API.

Newtonsoft.JSON . , .

+5

, Newtonsoft.Json http://www.newtonsoft.com/json

return Newtonsoft.Json.JsonConvert.DeserializeObject<SheetsService>(service);

JSON, : credential = Newtonsoft.Json.JsonConvert.DeserializeObject<credential.CreateScoped>(scopes);

0

You can check out this related GitHub thread . This workaround is:

install-package Microsoft.Bcl.Build
install-package Microsoft.Bcl.Async
install-package Microsoft.Net.Http

Alternatively, you can follow this tutorial on JSON serialization / deserialization in C # and see if you miss something.

0
source

Source: https://habr.com/ru/post/1664583/


All Articles