如何使用服务帐户创建Google Spreadsheet并在java中与其他Google用户共享? [英] How do I create a Google Spreadsheet with a service account and share to other google users in java?

查看:106
本文介绍了如何使用服务帐户创建Google Spreadsheet并在java中与其他Google用户共享?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,通过 Google服务帐户,从Google AnalyticsAPI收集有关我的网站的大量信息。
我的下一步是创建一个包含服务帐户
的电子表格,并与几个用户共享文档。



我已经检出了 https://developers.google.com/google-apps/spreadsheets/ 上的文档,但我无法找到有关服务帐户和共享文件的任何内容。

所以我的第一个问题是可能的?
如果没有,我需要执行文档中举例说明的使用我的个人帐户?
如果是的话,请给我一个例子吗?



谢谢!

解决方案

有可能的话,看下面的例子(这个例子确实需要调整一下):



创建驱动器服务:

  GoogleCredential凭证=新GoogleCredential.Builder()。setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
.setServiceAccountId(confBean.getServiceAccountId ())。setServiceAccountScopes( https://www.googleapis.com/auth/drive)
.setServiceAccountPrivateKeyFromP12File(新文件( 路径P12File))
.setServiceAccountUser(用户@ 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 = this.drive.files()。insert(file);
file = insert.execute();

创建电子表格服务:



<$ p $ 。p> GoogleCredential凭证=新GoogleCredential.Builder()setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
.setServiceAccountId(confBean.getServiceAccountId())setServiceAccountScopes(https://开头的电子表格。 google.com/feeds )
.setServiceAccountPrivateKeyFromP12File(新文件( 路径P12File ))
.setServiceAccountUser( user@domain.com)
.build();
SpreadsheetService service = new SpreadsheetService(MySpreadsheetIntegration-v1);
service.setOAuth2Credentials(凭证);

检索工作表:

  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); 
列表< WorksheetEntry>工作表= 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);

另请参阅问题


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.

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.

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!

解决方案

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

这篇关于如何使用服务帐户创建Google Spreadsheet并在java中与其他Google用户共享?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆