错误403尝试从Google云端硬盘下载文件时被禁止

时间:2014-03-14 14:12:00

标签: java google-drive-api

我正在使用Google云端硬盘,但我不时收到来自Google的错误说:(但只在尝试下载文件时。删除或添加文件仍然有效)

Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code": 403,
  "errors": [
    {
      "domain": "usageLimits",
      "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
      "reason": "dailyLimitExceededUnreg",
      "extendedHelp": "https://code.google.com/apis/console"
    }
  ],
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}


以下是我用来从Google云端硬盘获取文件流的代码:

    Credential credential = new GoogleCredential.Builder().setJsonFactory(GsonFactory.getDefaultInstance())
       .setTransport(getTransport()).setClientSecrets(secrets).build().setAccessToken(accessToken);
    Drive drive = Drive.Builder(getTransport(), GsonFactory.getDefaultInstance(), 
       getCredential(accessCode, googleClientSecrets)).setApplicationName("XXX").build();
    File file = drive.files().get(googleDocId).execute();
    String url = file.getExportLinks().get(googleMimeType);
    return drive.getRequestFactory().buildGetRequest(new GenericUrl(url)).execute().getContent();

1 个答案:

答案 0 :(得分:0)

“超出未经身份验证的使用限制。持续使用需要注册。”是误导。这意味着你已达到某种配额,但几乎不是这样。

未经身份验证的云端硬盘配额为零。所以消息真正告诉你的是你的Drive请求没有携带Auth令牌。您应该跟踪http,并且您将发现失败的请求缺少Authorization标头。至于为什么,这对读者来说是一种练习。