permissions - 当 Service Account 创建 Google Sheets 时,需要权限。我怎样才能给予许可?

标签 permissions google-sheets-api

我正在使用 Google Sheet API (V4)。

我的 Google 帐户类似于“test@google.com”。为了使用 google api,我在 Google 控制台中创建了一个项目并创建了服务帐户。

我想创建一个只有授权人员和创建该表格的我自己才能访问的 Google 表格。授权人可以阅读和编辑。我也不。

但每当我厌倦我的代码时,它运行良好,我可以获得工作表 url。但是当我点击他们显示的网址时,我需要一个许可。 这是我创建的 url [ https://docs.google.com/spreadsheets/d/1RKR-ErUaC_LujUUDf8o_yIprEz223U1EltJ7zYPo7us/edit]

我认为问题在于我正在使用 OAuth 服务帐户。我需要用这个。

所以.. 我想创建 google 表格并授予我选择的人阅读和编辑的权限。

请帮帮我...!

 /**
 * Application name.
 */
private static final String APPLICATION_NAME = "Google Sheets API Java";

/**
 * Global instance of the JSON factory.
 */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/**
 * Global instance of the HTTP transport.
 */
private static HttpTransport HTTP_TRANSPORT;

/**
 * Global instance of the scopes required by this quickstart.
 * <p>
 * If modifying these scopes, delete your previously saved credentials
 * at ~/.credentials/sheets.googleapis.com-java-quickstart
 */
private static final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS);

static {
    try {
        HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }
}

/**
 * Creates an authorized Credential object.
 *
 * @return an authorized Credential object.
 * @throws IOException
 */
public Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = GoogleSheetConfig.class.getResourceAsStream("/My Service Account Information.json");
    GoogleCredential credential = GoogleCredential
            .fromStream(in)
            .createScoped(SCOPES);
    return credential;
}

/**
 * Build and return an authorized Sheets API client service.
 *
 * @return an authorized Sheets API client service
 * @throws IOException
 */
@Bean
public Sheets getSheetsService() throws Exception {
    Credential credential = authorize();
    return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
}

我的服务帐户信息如下:

{
"type": "service_account",
"project_id": "my project id ",
"private_key_id": "fb925c0954********",
"private_key": 
"client_email": "test-service-accoun@admin-auth-
test.iam.gserviceaccount.com",
"client_id": "my client id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": 
"https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": 
"https://www.googleapis.com/robot/v1/metadata/x509/admin-auth-test-
service-accoun%40admin-auth-test.iam.gserviceaccount.com"
}

下面是我创建工作表的测试代码:

Spreadsheet requestBody = new Spreadsheet();

Sheets.Spreadsheets.Create request = sheets.spreadsheets().create(requestBody);

Spreadsheet response = request.execute();
System.out.println(response);

最佳答案

您是对的,因为您使用的是服务帐户,因此它创建的文件只能由该帐户访问。如本tutorial所述关于服务帐户:

Using a service account to access Google Drive API can be very useful but it is important to remember that a service account is not you. If you want to be able to access the files it uploads you must grant yourself access to them though the service account.

为自己添加对文件的权限:

  1. 使用云端硬盘 API
  2. 使用Permissions: updatePermissions: create

我建议您使用 Drive API 创建工作表以在工作表创建中添加权限。然后使用 Sheets API 编辑内容。

希望这对您有所帮助。

关于permissions - 当 Service Account 创建 Google Sheets 时,需要权限。我怎样才能给予许可?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44395567/

相关文章:

将 Google Sheets API 与服务帐户一起使用时出现 C# 错误

powershell - 无法写入 Google 表格((400)错误请求)

database - PostgreSQL 9.6数据库上的关联权限被拒绝

google-sheets - 如何将 Google 电子表格的工作表字符串 id 转换为整数索引 (GID)?

google-apps-script - 如何请求 updateCells 使多行具有相同的值

javascript - 气体 : Unable to use HtmlService custom dialogs in Firefox

wordpress - 禁止您无权访问此服务器上的/wp-login.php

powershell - 使用 powershell_script 自动导入 mediawiki

android 权限 vs linux 权限

java - hdfs文件权限