【问题标题】:403 quotaExceeded error when executing com.google.api.services.drive.Drive.Files.Insert on Android, using a service account使用服务帐户在 Android 上执行 com.google.api.services.drive.Drive.Files.Insert 时出现 403 quotaExceeded 错误
【发布时间】:2015-01-28 23:07:39
【问题描述】:

我有一个 Android 应用,它使用服务帐户复制电子表格、编辑电子表格并将一些照片上传到第三方 Google 云端硬盘帐户。

应用程序运行正常。但在 1 月 22 日的最后一天,应用程序开始出现故障。

现在它会复制电子表格并进行编辑,但不再上传照片。 我收到了这个错误。

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden

{re 
  "code" : 403,
  "errors" : [ {
    "domain" : "usageLimits",
    "message" : "The user has exceeded their Drive storage quota",
    "reason" : "quotaExceeded"
  } ],
  "message" : "The user has exceeded their Drive storage quota"
}

我检查了 API 配额和云端硬盘存储配额,它们都远低于限制。

我没有更改代码,也没有更改任何相关帐户的安全设置。

我找不到任何关于此的已知问题。

还有其他人遇到过类似的情况吗?

【问题讨论】:

标签: google-drive-api http-status-code-403 quota


【解决方案1】:

我找到了问题和候选解决方案。

这是场景:

  • 可以访问 Google 云端硬盘的帐户,我们称之为:manager@gmail.com
  • 用于将照片上传到经理 Google Drive 的多个帐户,使用 Android 应用程序,我们称之为:worker@gmail.com
  • 我不想让 workers@gmail.com 访问 manager@gmail.com 的 Google 云端硬盘,因此我创建了一个服务帐户,它负责将照片从 Android App 上传到管理器 Google Drive,我们称之为 service@gmail.com

worker@gmail.com 拍照时,service@gmail.com 会将照片上传到 manager@gmail.com谷歌云端硬盘。但照片的所有者是 service@gmail.com。因此,在 manager@gmail.com Google Drive 中,您可以看到所有照片,但配额始终保持为零。

上传 15 GB o 照片后,我实际上超出了配额,但这是 service@gmail.com 的配额(在 Google Drive 中不可见)。

解决方案

  1. 登录 manager@gmail.com Google Drive 并删除照片。

    错误: manager@gmail.com 不是所有者。因此,如果您删除该文件,manager 将看不到它,但它会继续消耗 service@gmail.com 的配额。

  2. 登录 service@gmail.com Google Drive 并删除照片。

    错误:无法 (AKAIK) 登录 service@gmail.com Google 云端硬盘。

  3. 将文件的所有权从 service@gmail.com 转移到 manager@gmail.com

    COOL 这是一个有效的解决方案。让我们去做吧!

    错误 Google 似乎不允许更改某些类型文件的所有权。 More info here

肮脏的解决方案

是的,这可能不是最优雅的解决方案。但它对我有用。

我使用 manager@gmail.com 帐户创建了一个 Google 脚本,并作为公共 Web 应用程序发布。

function doGet(data) {
  var fileId = data.parameter.fileId;
  var file = DriveApp.getFileById(fileId);
  var result = { fileId: file.makeCopy(file.getName()).getId() };
  //I need to remove the file. Otherwise it semains linked to the one created by the *service@gmail.com*
  DriveApp.removeFile(file);
  return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(ContentService.MimeType.JSON);
}

此方法接收 service@gmail.com 创建的 fileId,制作一个副本(拥有 manager@gmail.com)。我需要删除 service@gmail.com 创建的文件。该文件实际上并未被删除,但它不再与 manager@gmail.com 创建的文件相关联。最后返回新的fileId。

现在,我的 Android 应用如下所示:

private void uploadPhoto(String photoFolderId, File body, FileContent mediaContent) {
    //First, I upload the photo
    if(driveService.files().insert(body, mediaContent) != null) {
        transferOwnerShip(photoFolderId);
    }

    private void transferOwnerShip(String photoFolderId) {
        String photoId = getInsertedPhotoId();
        //Now I create a copy of the photo. The copied photo is owned by *manager@gmail.com*
        String ownedPhotoId = makeCopyOfPhoto(photoId);
        if(ownedPhotoId != null)
            //Once I have copied the photo, I remove the photo owned by *service@gmail.com*
            driveService.files().delete(photoId).execute();
    }

    private String makeCopyOfFile(String fileId) {
        URL url;
        try {
            url = new URL(URL_TO_MY_PUBLIC_SERVICE + "?fileId=" + fileId);

            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(url.toURI());
            HttpResponse resp = client.execute(get);
            BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            JSONObject jsonResponse = new JSONObject(result.toString());
            return jsonResponse.getString("fileId");
        } catch (Exception e) {
            warnAboutError();
        }
        return null;
    }

}

肮脏的解决方案有效,但非常肮脏。我想不出其他方法来解决这个问题。

【讨论】:

    猜你喜欢
    • 2020-04-10
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    相关资源
    最近更新 更多