Google 工作表 API:设置权限
Google Sheets API: Setting permissions
在我的 Java 应用程序中,我正在创建新的 Google sheets,如下所示:
Sheets service = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
SpreadsheetProperties properties = new SpreadsheetProperties();
properties.setTitle("Title");
Spreadsheet export = new Spreadsheet();
export.setProperties(properties);
Spreadsheet response = service.spreadsheets().create(export).execute();
这有效,我可以在响应中获得 sheet ID。现在我想知道:
- 因为我使用的是服务帐户,所以我看不到创建点差的用户是谁sheet。
- 我想为 "Anyone who has the link" 设置权限,但似乎没有相应的选项。
因为我使用的是服务帐户,所以我看不到创建电子表格的用户是谁。
您可以使用 "user" 作为类型:
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();
但这实际上取决于您的最终目标。这是可能的 Types and Values.
user - emailAddress - 用户的电子邮件地址。示例:joe@thecompany.com
group - emailAddress - Google 群组的电子邮件地址。示例:admins@thecompany.com 域-
domain - Google Apps 域的域名。示例:thecompany.com 任何人 - N/A
anyone - 权限不需要电子邮件地址或域字段。
检查此 SO thread 以获取更多信息。
我想将权限设置为 "Anyone who has the link",但似乎没有相应的选项。
使用"anyone"作为上面提到的用户类型。然后使用 "reader" 作为角色。
private Permission insertPermission(Drive service, String fileId) throws Exception{
Permission newPermission = new Permission();
newPermission.setType("anyone");
newPermission.setRole("reader");
newPermission.setValue("");
newPermission.setWithLink(true);
return service.permissions().insert(fileId, newPermission).execute();
}
检查这个有用 SO thread。
在我的 Java 应用程序中,我正在创建新的 Google sheets,如下所示:
Sheets service = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
SpreadsheetProperties properties = new SpreadsheetProperties();
properties.setTitle("Title");
Spreadsheet export = new Spreadsheet();
export.setProperties(properties);
Spreadsheet response = service.spreadsheets().create(export).execute();
这有效,我可以在响应中获得 sheet ID。现在我想知道:
- 因为我使用的是服务帐户,所以我看不到创建点差的用户是谁sheet。
- 我想为 "Anyone who has the link" 设置权限,但似乎没有相应的选项。
因为我使用的是服务帐户,所以我看不到创建电子表格的用户是谁。
您可以使用 "user" 作为类型:
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();
但这实际上取决于您的最终目标。这是可能的 Types and Values.
user - emailAddress - 用户的电子邮件地址。示例:joe@thecompany.com
group - emailAddress - Google 群组的电子邮件地址。示例:admins@thecompany.com 域-
domain - Google Apps 域的域名。示例:thecompany.com 任何人 - N/A
anyone - 权限不需要电子邮件地址或域字段。
检查此 SO thread 以获取更多信息。
我想将权限设置为 "Anyone who has the link",但似乎没有相应的选项。
使用"anyone"作为上面提到的用户类型。然后使用 "reader" 作为角色。
private Permission insertPermission(Drive service, String fileId) throws Exception{
Permission newPermission = new Permission();
newPermission.setType("anyone");
newPermission.setRole("reader");
newPermission.setValue("");
newPermission.setWithLink(true);
return service.permissions().insert(fileId, newPermission).execute();
}
检查这个有用 SO thread。