Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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
如何创建一个带有服务帐户的谷歌电子表格,并用java与其他谷歌用户共享?_Java_Google Api_Google Spreadsheet Api - Fatal编程技术网

如何创建一个带有服务帐户的谷歌电子表格,并用java与其他谷歌用户共享?

如何创建一个带有服务帐户的谷歌电子表格,并用java与其他谷歌用户共享?,java,google-api,google-spreadsheet-api,Java,Google Api,Google Spreadsheet Api,我有一个应用程序,我使用谷歌服务帐户,从分析API收集大量关于我网站的信息。 我的下一步是使用服务帐户创建电子表格 并与几个用户共享文档 我已经在上查看了文档,但在那里找不到任何关于服务帐户和共享文档的信息 那么我的第一个问题是这可能吗? 如果没有,我是否需要执行文档中举例说明的“使用我的个人帐户”? 如果是,你能给我举个例子吗 谢谢大家! 这是可能的,请参见下面的示例(该示例确实需要一些调整): 创建驱动器服务: GoogleCredential credential = new Goo

我有一个应用程序,我使用谷歌服务帐户,从分析API收集大量关于我网站的信息。 我的下一步是使用服务帐户创建电子表格 并与几个用户共享文档

我已经在上查看了文档,但在那里找不到任何关于服务帐户和共享文档的信息

那么我的第一个问题是这可能吗? 如果没有,我是否需要执行文档中举例说明的“使用我的个人帐户”? 如果是,你能给我举个例子吗


谢谢大家!

这是可能的,请参见下面的示例(该示例确实需要一些调整):

创建驱动器服务:

   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("user@domain.com")
            .build();

    Drive drive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build();
创建电子表格:

  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();
创建电子表格服务:

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("user@domain.com")
        .build();
SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
service.setOAuth2Credentials(credential);
检索工作表:

SpreadsheetService s = googleConn.getSpreadSheetService();
String spreadsheetURL = "https://spreadsheets.google.com/feeds/spreadsheets/" + file.getId();
SpreadsheetEntry spreadsheet = s.getEntry(new URL(spreadsheetURL), SpreadsheetEntry.class);
添加数据:

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);
WorksheetFeed WorksheetFeed=s.getFeed(电子表格.getWorksheetFeedUrl(),WorksheetFeed.class);
List worksheets=worksheetFeed.getEntries();
WorksheetEntry工作表=worksheets.get(0);
URL cellFeedUrl=worksheet.getCellFeedUrl();
CellFeed CellFeed=s.getFeed(cellFeedUrl,CellFeed.class);
CellEntry CellEntry=新的CellEntry(1,1,“aa”);
cellFeed.insert(cellEntry);

另外,请参阅此相关的

谢谢您的回答,但我在impl中遇到了一些问题。执行setServiceAccountUser(“user@domain.com)我从API中得到一个TokenResponseException:400错误请求“错误”:“访问被拒绝”。如果删除setServiceAccountUser部分,则会出现500内部服务器错误。我已经在谷歌服务控制台中启用了谷歌驱动API和谷歌驱动SDK。我错过了什么?正如我在这里所描述的,分析没有问题:确保在api控制台中同时启用驱动器和文档api,并为以下应用程序授权:。我猜你改变了主意user@domain.com对于域中的真正用户来说?嗯,我觉得我缺少了一些关键部分。现在我得到了com.google.api.client.auth.oauth2.TokenResponseException:400错误请求{“错误”:“无效的授权”}。我使用与分析相同的客户端id:2363681XX719.apps.googleusercontent.com和user@domain.com我试过了2363681XX719@developer.gserviceaccount.com和一封在我的域名注册的电子邮件。也许这就是问题所在,我如何检查用户在我的域中是否正确?我还需要什么吗?也许我忘了什么?代码中有两个部分,创建电子表格和操作它。你从哪里得到的错误?你尝试过什么最终的解决方案吗?