C# 如何在.NETCore中使用GoogleSheets API?
我正在尝试使用ASP.NETCore中的c#创建和编辑google表单 目前,我可以使用以下代码编辑工作表:C# 如何在.NETCore中使用GoogleSheets API?,c#,google-api,asp.net-core,C#,Google Api,Asp.net Core,我正在尝试使用ASP.NETCore中的c#创建和编辑google表单 目前,我可以使用以下代码编辑工作表: var credential = GoogleCredential. FromStream(new FileStream("Google/client_secrets.json", FileMode.Open)). CreateScoped(SheetsService.Scope.Spreadsheets); var service
var credential = GoogleCredential.
FromStream(new FileStream("Google/client_secrets.json", FileMode.Open)).
CreateScoped(SheetsService.Scope.Spreadsheets);
var service = new SheetsService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "just a test"
});
// service.Spreadsheets.Values.Update(valueRangeNames, spreadsheetId, rangeNames);
UserCredential credential;
using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
如您所见,我使用的是服务帐户
在google的.NET快速入门指南中,他们使用以下代码:
var credential = GoogleCredential.
FromStream(new FileStream("Google/client_secrets.json", FileMode.Open)).
CreateScoped(SheetsService.Scope.Spreadsheets);
var service = new SheetsService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "just a test"
});
// service.Spreadsheets.Values.Update(valueRangeNames, spreadsheetId, rangeNames);
UserCredential credential;
using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
但是,在.NETCore中,GoogleWebAuthorizationBroker并不存在。
据我所知,它用于使用Oauth2对google帐户进行身份验证
我也在这里的某个地方读到过,所以你不能使用sheet api创建google工作表。你需要使用谷歌驱动api。
我使用了GoogleDrive api(增益,使用服务帐户进行身份验证)。我可以创建一个文件,但我不知道如何访问它。我知道我创建了一个文件,因为他们在api上给出的示例有一个小方法显示了该服务帐户中的所有文件
长话短说:
假设我有一个应用程序的google帐户。我想使用此帐户使用C#.NetCore创建多张工作表。
如果可能的话,我希望这些工作表只能由某些人编辑。如果不是,则仅从应用程序。
其他所有人只有查看权限
我该怎么做?服务帐户对我计划做的事情有用吗?如果没有,如何使用.Net Core对api进行身份验证
在你问之前,是的,我搜索了(很多)Google WebAuthorizationBroker的替代者,但我无法找到。我从未使用过谷歌API,我也不是一个经验丰富的程序员。尽管如此,使用标准的.NET代码,google quickstart指南上的所有示例都能正常工作。为什么它们不能在.NETCore中工作
谢谢。GoogleWebAuthorizationBroker似乎使用了不友好的.Net核心方法,但还有其他身份验证方法 首先,您需要应用程序的刷新令牌。如果您以前使用代码进行过授权,它将位于
AppData\Roaming\GoogleCredentials\Google.api.Auth.OAuth2.Responses.TokenResponse user
(使用您喜爱的文本编辑器打开)。否则,请参阅以获取应用程序刷新令牌
然后你可以这样授权
var token = new TokenResponse { RefreshToken = tokenYouJustGot };
UserCredential credentials;
using (var stream = File.OpenRead("client_secret.json"))
{
credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer()
{
ClientSecretsStream = stream
}), "user", token);
}
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credentials,
ApplicationName = "Sample Application"
});
在获得表格服务后,您可以继续制作和编辑电子表格。
例如,要创建新的电子表格,可以执行var sheet=service.Spreadsheets.create(new spreadsheet())代码>