Google Sheets API:设置权限 [英] Google Sheets API: Setting permissions

查看:386
本文介绍了Google Sheets API:设置权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java应用程序中,我正在按以下方式创建新的Google表格:

Within my Java application I am creating new Google sheets as follows:

    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();

这有效,我可以在响应中获取工作表ID.现在我想知道:

This works, and I can get the sheet ID in the response. Now I would like to know:

  1. 在使用服务帐户时,我看不到谁将是创建电子表格的用户.
  2. 我想为知道链接的任何人"设置权限,但是似乎没有任何选择.

推荐答案

在使用服务帐户时,我看不到谁将是创建电子表格的用户.

您可以使用用户"这样的类型:

You can make use of "user" as type like:

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();

但这确实取决于您的最终目标.以下是可能的类型和值.

But it really depends on your end goal. Here are the possible Types and Values.

用户 - emailAddress -用户的电子邮件地址.示例:joe@thecompany.com

user - emailAddress - Email address of a user. Example: joe@thecompany.com

- emailAddress -Google网上论坛的电子邮件地址.例如:admins@thecompany.com域-

group - emailAddress - Email address of a Google Group. Example: admins@thecompany.com domain-

-Google Apps域的域名.示例:thecompany.com任何人-N/A The

domain - Domain name of Google Apps domain. Example: thecompany.com anyone - N/A The

任何人 -权限不需要emailAddress或域字段.

anyone - permission does not require an emailAddress or domain field.

选中此

Check this SO thread for addtional info.

我想为知道链接的任何人"设置权限,但是似乎没有任何选择.

使用任何人"作为上述用户类型.然后使用阅读器"作为角色.

Use "anyone" as user type which has been mentioned above. Then use "reader" as role.

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线程.

这篇关于Google Sheets API:设置权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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