Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
反序列化JSON凭证数据C#Google Sheets API时出错_C#_Json_Google Api_Google Sheets Api_Service Accounts - Fatal编程技术网

反序列化JSON凭证数据C#Google Sheets API时出错

反序列化JSON凭证数据C#Google Sheets API时出错,c#,json,google-api,google-sheets-api,service-accounts,C#,Json,Google Api,Google Sheets Api,Service Accounts,我目前正试图使用一个服务帐户来访问GoogleSheets API——我遇到的问题是我的.json文件 这是我的密码: try { string[] scopes = new string[] { SheetsService.Scope.Spreadsheets, SheetsService.Scope.SpreadsheetsReadonly }; // Put your scopes here var stream = new FileStr

我目前正试图使用一个服务帐户来访问GoogleSheets API——我遇到的问题是我的.json文件

这是我的密码:

    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);
    }
这引发了我的错误,即:

Create service account myApplicationServiceAccount failed : Error deserializing JSON credential data.
但我能在网上找到的一切都表明我上面所说的应该有用


这个.json文件还有什么需要我做的吗

尝试使用
Newtonsoft.Json
库反序列化对象
http://www.newtonsoft.com/json

返回Newtonsoft.Json.JsonConvert.DeserializeObject(服务)

但错误在于反序列化JSON凭据数据,因此这可能会有所帮助:
credential=Newtonsoft.Json.JsonConvert.DeserializeObject(作用域)

您可以检查这个。给定的解决方法是:

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

另外,您可以在C#中关注JSON序列化/反序列化,看看是否遗漏了什么。

事实证明,因为我支持.NET的旧版本(4及以下),所以我使用的是Google Sheets API的稍旧版本


但是Newtonsoft.JSON包也过时了。更新后,它现在开始工作。

它说“无法从”Google.api.Sheets.v4.SheetsService“转换为”string“。我觉得我可能需要将反序列化的重点放在my_application_secret.json文件上,而不是Newtonsoft.json.JsonConvert.DeserializeObject(scopes)