如何使用服务帐户创建 Google 电子表格并在 Java 中与其他 Google 用户共享?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13229294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a Google Spreadsheet with a service account and share to other google users in java?
提问by jakob
I have an application where I, with a Google Service Account, gather lots of information about my site from the Analytics API. My next step is to create a spreadsheet with the service account and share the document with a couple of users.
我有一个应用程序,我使用Google 服务帐户从 Analytics API 收集有关我网站的大量信息。我的下一步是使用服务帐户创建一个电子表格,并与几个用户共享该文档。
I have checked out the documentation at https://developers.google.com/google-apps/spreadsheets/but I can't find anything there about service accounts and sharing the documents.
我已在https://developers.google.com/google-apps/spreadsheets/ 上查看了文档,但在那里找不到有关服务帐户和共享文档的任何信息。
So my first question is this possible? If not do I need to implement the "use my personal account" as exemplified in the documentation? If yes could you please provide me with an example?
所以我的第一个问题是这可能吗?如果不是,我是否需要实现文档中举例说明的“使用我的个人帐户”?如果是的话,你能给我举个例子吗?
Thank you!
谢谢!
采纳答案by Jasper Duizendstra
It is possible, see the example below (the example does need a bit of tweaking):
有可能,请参见下面的示例(该示例确实需要进行一些调整):
Create the drive service:
创建驱动服务:
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
.setServiceAccountId(confBean.getServiceAccountId()).setServiceAccountScopes("https://www.googleapis.com/auth/drive")
.setServiceAccountPrivateKeyFromP12File(new File("path to the P12File"))
.setServiceAccountUser("[email protected]")
.build();
Drive drive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build();
Create the spreadsheet:
创建电子表格:
com.google.api.services.drive.model.File file = new com.google.api.services.drive.model.File();
file.setTitle("test");
file.setMimeType("application/vnd.google-apps.spreadsheet");
Insert insert = this.drive.files().insert(file);
file = insert.execute();
Create a spreadsheet service:
创建电子表格服务:
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
.setServiceAccountId(confBean.getServiceAccountId()).setServiceAccountScopes("https://spreadsheets.google.com/feeds")
.setServiceAccountPrivateKeyFromP12File(new File("path to the P12File"))
.setServiceAccountUser("[email protected]")
.build();
SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
service.setOAuth2Credentials(credential);
Retrieve the sheet:
检索工作表:
SpreadsheetService s = googleConn.getSpreadSheetService();
String spreadsheetURL = "https://spreadsheets.google.com/feeds/spreadsheets/" + file.getId();
SpreadsheetEntry spreadsheet = s.getEntry(new URL(spreadsheetURL), SpreadsheetEntry.class);
Add the data:
添加数据:
WorksheetFeed worksheetFeed = s.getFeed(spreadsheet.getWorksheetFeedUrl(), WorksheetFeed.class);
List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
WorksheetEntry worksheet = worksheets.get(0);
URL cellFeedUrl= worksheet.getCellFeedUrl ();
CellFeed cellFeed= s.getFeed (cellFeedUrl, CellFeed.class);
CellEntry cellEntry= new CellEntry (1, 1, "aa");
cellFeed.insert (cellEntry);
Also, see this related question
另外,请参阅此相关问题