C# 使用.NETC中的最新API(v4)在另一个&x27;中创建Google电子表格;s账户
我无法使用新的电子表格API在GoogleDocs中创建电子表格,因为它现在使用GoogleDrive API得到支持 我找到的所有示例都是用于创建和修改工作表的,而不是主电子表格C# 使用.NETC中的最新API(v4)在另一个&x27;中创建Google电子表格;s账户,c#,.net,google-sheets,google-docs-api,C#,.net,Google Sheets,Google Docs Api,我无法使用新的电子表格API在GoogleDocs中创建电子表格,因为它现在使用GoogleDrive API得到支持 我找到的所有示例都是用于创建和修改工作表的,而不是主电子表格 static string[] Scopes = { SheetsService.Scope.Spreadsheets }; var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId =
static string[] Scopes = { SheetsService.Scope.Spreadsheets };
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets {
ClientId = clientId, // FROM JSON
ClientSecret = clientSecret // FROM JSON
},
Scopes, Environment.UserName, CancellationToken.None,
new FileDataStore("xxIDxx.GoogleDrive.Auth.Store")).Result;
var service = new SheetsService(new BaseClientService.Initializer() {
HttpClientInitializer = credential,
ApplicationName = "Google Sheets API Project",
});
string SpreadSheetID = "AVeryLongAndRandomStringID";
Spreadsheet SpSheet = new Spreadsheet();
SpSheet.Properties = new SpreadsheetProperties();
SpSheet.SpreadsheetId = SpreadSheetID;
SpSheet.Properties.Title = "I HATE THIS SPREADSHEET";
Sheet MySheet = new Sheet();
MySheet.Properties = new SheetProperties();
MySheet.Properties.Title = "MySheet";
MySheet.Properties.SheetId = 34213312;
MySheet.Properties.SheetType = "GRID";
var SheetSet = new List<Sheet>();
SheetSet.Add(MySheet);
SpSheet.Sheets = SheetSet;
var MyNewSpreadSheet = service.Spreadsheets.Create(SpSheet).Execute();
static string[]Scopes={SheetsService.Scope.Spreadsheets};
var-credential=GoogleWebAuthorizationBroker.AuthorizationAsync(新客户端机密){
ClientId=ClientId,//来自JSON
ClientSecret=ClientSecret//来自JSON
},
作用域,Environment.UserName,CancellationToken.None,
新文件数据存储(“xxIDxx.GoogleDrive.Auth.Store”)。结果;
var service=new SheetsService(new BaseClientService.Initializer(){
HttpClientInitializer=凭证,
ApplicationName=“Google Sheets API项目”,
});
字符串SpreadSheetID=“Averylongandromstringid”;
电子表格SpSheet=新电子表格();
SpSheet.Properties=新的电子表格属性();
SpSheet.SpreadsheetId=电子表格ID;
SpSheet.Properties.Title=“我讨厌这个电子表格”;
工作表MySheet=新工作表();
MySheet.Properties=新的SheetProperties();
MySheet.Properties.Title=“MySheet”;
MySheet.Properties.SheetId=34213312;
MySheet.Properties.SheetType=“网格”;
var SheetSet=新列表();
添加(MySheet);
SpSheet.Sheets=图纸集;
var MyNewSpreadSheet=service.Spreadsheets.Create(SpSheet.Execute();
谢谢
更新:
小版本的“var MyNewSpreadSheet”确实有效(我上次的尝试也有效),但。。。我没有意识到它是在我的谷歌硬盘中保存文档,而不是在我客户的帐户中保存文档
我试图实现的是创建一个应用程序,任何拥有google帐户的人都可以在“存储库”帐户中创建或更改电子表格文档
“client_secret.json”文件是从我客户的帐户生成的,我不知道为什么代码会在登录的gmail帐户上创建电子表格
有什么想法吗?
谢谢 您可能希望尝试使用以下格式发送HTTP请求:
POST https://sheets.googleapis.com/v4/spreadsheets
如中所述,如果请求成功,响应主体将包含新创建的电子表格实例。
另外,请注意,使用此方法需要以下OAuth作用域中的一个:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/spreadsheets
此外,如果有帮助,请尝试检查此项。这是关于使用C#的Sheets API。示例代码提到一个需要删除的令牌文件,该文件不存在,必须进行“清理”,然后更改行
静态字符串[]Scopes={SheetsService.Scope.spreadsheetsreadoly}编码>并将电子表格重新替换为驱动器