反序列化JSON凭证数据C#Google Sheets API时出错
我目前正试图使用一个服务帐户来访问GoogleSheets API——我遇到的问题是我的.json文件 这是我的密码:反序列化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
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)不幸的是,这给了我同样的结果。此外,您上面关于凭证的更新也不起作用-给我一个错误:“凭证是一个变量,但使用起来像一个类型”,这些都是通过Nu Get安装的;还有其他地方我需要包括这些吗?